<分区>
标签 javascript jquery html css this
<分区>
我希望能够为类创建“点击”响应,然后更改被点击的特定元素的 CSS。
这似乎不起作用。我认为使用 $(this)
会指示特定元素,但它似乎没有。一个复杂的问题是我正在覆盖外部样式表。
我的 jquery 代码:
$('.myClass').click( function() {
$('this').css("width: 30% !important");
alert("THIS ALERT WORKS");
})
警报确实有效。我有能够更改 .myClass
的静态 CSS。
我的 HTML:
<button class="classA classB myClass" type="submit">Button</button>
我也试过 $('this').val("")
但这行不通。
最佳答案
试试这个
$('.myClass').click( function(){
$(this).css('width', '30%');
// or $(this).css('width', '30% !important');
})
你的代码有两个错误
1.
$('this') should be $(this)
2.
$('this').css("width: 30% !important");
应该是 $('this').css("width", "30% !important");
如果你想改变不止一个选项你可以这样做
$(this).css({backgroundColor: "green", borderColor: "black", borderWidth: "30px"});
//或
$(this).css({'background-color': "green", 'border-color': "black", 'border-width': "30px"});
$.css - on this page你可以找到关于 $.css
语法的很好的解释
关于javascript - 如何根据类 jquery 回调函数更改单个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27726806/