开发者问题收集

Javascript 未捕获的类型错误:无法读取未定义的属性“style”(…)

2016-11-26
728

您好,我有以下代码:

我希望能够在单击输入时更改其背景颜色。以及执行其他操作。

var bp = false;
    	var np = false;
    	var b = false;
    	var i = false;
    	var u = false;
    
    function iFrameOn(){
        richTextField.document.designMode = 'On';
    }
    function iBold(){
    	 richTextField.document.execCommand('Bold',false,null);
    	 if(b == false){
    	 	b = true;
    	 	document.getElementById['btn1'].style.backgroundColor = 'red';
    	 }else if(b == true){
    	 	b = false;
    	 	document.getElementById['btn1'].style.backgroundColor = 'red';
    	 }
    }
    
<center> 
              <div>
                <input class="buttonOnBar" id="btn1" type="button" value="" onClick="javascript:iBold();" style="background-image: url('img/icons/bold.png');" title="Bold text">
                <input class="buttonOnBar" id="btn2" type="button" value="" onClick="javascript:iUnderline();" style="background-image: url('img/icons/underline.png');"title="Underlined text">
                <input class="buttonOnBar" id="btn3" type="button" value="" onClick="javascript:iItalic();" style="background-image: url('img/icons/italic.png');"title="Italic text">
                <input class="buttonOnBar" id="btn4" type="button" value="" onClick="javascript:iUnorderedList();" style="background-image: url('img/icons/numberPoint.png');"title="Numbered list">
                <input class="buttonOnBar" id="btn5" type="button" value="" onClick="javascript:iOrderedList();" style="background-image: url('img/icons/bulletPoint.png');"title="Bullet point list">
                <input class="buttonOnBar" id="btn6" type="button" value="" onClick="javascript:iLink();" style="background-image: url('img/icons/link.png');"title="Insert link">
                <input class="buttonOnBar" id="btn7" type="button" value="" onClick="javascript:iImage();" style="background-image: url('img/icons/picture.png');"title="Insert image">
              </div>
            </center>

              <textarea style="display:none;"id="postTextStyle" name="Text" cols="100" rows="14"></textarea>
              <center><iframe name="richTextField" id="richTextField" style="border:#000000 1px solid; width:700px;height:300px;"></iframe></center>

              <textarea id="postTagStyle" name="desc" rows="2" maxlength = "1000" placeholder="Description"></textarea>

              <center><input type="button" value="Post"  style="margin-top:40px;"class = "Post2" onClick="javascript:submit_form();"></center>
           
           </form>
          
          <br>
          <script type="text/javascript"src="WYSIWYG.js"></script>

无论出于什么原因,当我单击输入按钮时,我总是收到标题中的错误。为什么我会收到此错误并且为什么颜色没有改变?

1个回答

您的意思是

document.getElementById('btn1').style.backgroundColor = 'red';
Mojo Allmighty
2016-11-26