gpt4 book ai didi

javascript - jQuery,使用字符串和变量将元素添加到匹配元素集中

转载 作者:行者123 更新时间:2023-11-29 18:25:33 27 4
gpt4 key购买 nike

我有一个点击事件,我想将一个类添加到当前点击的元素加上另一个元素,我想在一行中完成。

我试过类似的东西

JS文件

$('div').click(function(){
$(this, 'span').css('background','yellow');
// could usethe following way but it's not DRY
// $(this).css('background','yellow');
// $('span').css('background','yellow');
})​;

HTML 文件

<div>
div
</div>
<span>
span
</span>​

这里是 a fiddle例如

但它不起作用,所以我怎么能不重复 css 方法呢。

最佳答案

您可以像这样使用 .add(selector) 方法:

$(this).add('span').css('background','yellow');

关于javascript - jQuery,使用字符串和变量将元素添加到匹配元素集中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13857252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com