2011-01-20 21 views
0

我对JQuery非常陌生,实际上这是我写过的第一个脚本。下面简单地找到所有具有类“TestDIV”的DIV,然后在发现其中的输入时执行一些操作。在JQuery中更改borderColor的问题

除了borderColor,它仍然是我最初设置的颜色。有人对此有何看法?我也非常欢迎有关如何改进我的代码的提示。

function hideAndShowJQ(show) { 
     var hideColor = "#DFDFDF"; 
     //Find DIVs and modify styling 
     var div = $('div.TestDIV'); //Find relevant divs 
     div.css('color', (show) ? "" : hideColor) //Change text colour 
      .find(':input').attr("disabled", !show) //Disable any inputs 
      .attr("borderColor", "red") //Change border colour of inputs 
      .attr("value", ""); //Clear any existing input text 
} 

回答

5

问题是borderColor不是元素的属性,它是一个CSS属性。

要更改css属性/值,请使用css()。此外,使用引号时,它的"border-color"borderColor(虽然作为@Felix克林指出,在下面的意见,没关系关于驼峰引号是):

div.css('color', (show) ? "" : hideColor) //Change text colour 
      .find(':input').attr("disabled", !show) //Disable any inputs 
      .css("border-color", "red") //Change border colour of inputs 
      .attr("value", ""); //Clear any existing input text 

假设你正在使用jQuery,并有要清除input元素,它可能是更容易使用val(),而不是attr()

.val(''); // sets the value of the input to an empty string. 
+2

不要紧,无论是`borderColor`或`边界color`。两者都可以工作。只有当你传递一个对象到`css`时,如果你使用非字符串键,它必须是`borderColor`。 – 2011-01-20 10:49:33

+0

@Felix Kling:真的吗?我总是认为它的工作方式与变量类似(因为它们不会被字符串内插)。不过,我不知道我为什么这么想。谢谢! – 2011-01-20 10:51:47

0

你申请的BORDERCOLOR变量是错误的。

$(this).css("borderColor","red"); 
1

应该是:$(this).css("borderColor","red")

2

这可能工作太:

$('#div-name :input').attr("disabled", !show); 

,因为我用同样的形式为文本框

$('#div-name :input').attr("style", ""); 
0

您可以使用速记符号波纹管在Chrome和Safari的边框样式,但Mozilla的不要”不支持它。

$(this).css("borderColor"); 

所以你必须使用更具体的符号浏览器的兼容性。你可以用这个来代替一个:

$(this).css("border-top-color"); 
$(this).css("border-right-color"); 

等等