简体   繁体   English

JavaScript边框颜色/颜色样式

[英]JavaScript border-color/color styling

I'd like to style 'input.submit' of a form (hover effect for IE) using JS and tried the following which doesn't work unfortunately. 我想使用JS为表单的“ input.submit”(IE的悬停效果)设置样式,并尝试了以下不幸的工作。

<!--[if IE]>
<script type="text/javascript">

// CHANGE SUBMIT STYLE
var foo = document.getElementByClass('input.submit');
foo.onmouseover = this.style.border-color='#000000'; this.style.color='#000000';
foo.onmouseout = this.style.border-color='#888888'; this.style.color='#888888';
foo.onclick = this.style.border-color='#000000'; this.style.color='#000000';

</script>
<![endif]-->

Could you please fix this for me? 你能帮我解决这个问题吗? TIA, Dan TIA,丹

It should be: 它应该是:

foo.onmouseover = function() {
    this.style.borderColor='#000000'; 
    this.style.color='#000000';
}

The answer is considerably more complicated if the elements are generated by an external javascript script. 如果元素是由外部javascript脚本生成的,则答案要复杂得多。 You'll need to find the element first, so the by id and class approaches won't work unless they already have one - see the type solution below. 您需要首先找到元素,因此by idclass方法将无法工作,除非它们已经拥有一个-请参见下面的type解决方案。

Find by id : id查找:

The following is preferred, you need to add an id to the input submit eg <input type="submit" id="submit"> to reference it by: 以下是首选,您需要在输入提交中添加一个ID,例如<input type="submit" id="submit">以引用它:

// CHANGE SUBMIT STYLE
var foo = document.getElementById('submit');
foo.onmouseover = function() {this.style.borderColor='#000000'; this.style.color='#000000';}
foo.onmouseout = function() {this.style.borderColor='#888888'; this.style.color='#888888';}
foo.onclick = function() {this.style.borderColor='#000000'; this.style.color='#000000';}

but the following should also work: 但是以下内容也应该起作用:

Find by class : class查找:

you'll need to specify a class eg <input type="submit" class="submit"> in this case. 在这种情况下,您需要指定一个类,例如<input type="submit" class="submit"> getElementsByClass doesn't look for the type , it looks for the class . getElementsByClass并不查找type ,而是查找class

<script type="text/javascript">

function getElementsByClass(node,searchClass,tag) {
  var classElements = new Array();
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("\b"+searchClass+"\b");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
return classElements;
}

// CHANGE SUBMIT STYLE
var foo = getElementsByClass(document.body, 'submit', 'input')[0];
foo.onmouseover = function() {this.style.borderColor='#000000'; this.style.color='#000000';}
foo.onmouseout = function() {this.style.borderColor='#888888'; this.style.color='#888888';}
foo.onclick = function() {this.style.borderColor='#000000'; this.style.color='#000000';}

</script>

Find by type : type查找:

You could find by type if you use the following: 如果使用以下内容,则可以按type查找:

function getElementByType(node,type,tag) {
  var els = node.getElementsByTagName(tag);
  for (var x=0, x<els.length; x++) {
    if (els[x].type && els[x].type.toLowerCase() == type) {
      return els[x]
    }
  }
}
var foo = getElementByType(document.body, 'submit', 'input')
...

What I would do is: 我要做的是:

<div id="externalElms">
    (<script> here)
</div>

then use var foo = getElementByType(document.getElementById('externalElms'), 'submit', 'input') so it doesn't have to go through every element on the page. 然后使用var foo = getElementByType(document.getElementById('externalElms'), 'submit', 'input')这样就不必遍历页面上的每个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM