jQuery 将 find/if 限制为仅一个 div 及其子元素
这个问题很简单,但是设置有点复杂。请耐心等待...
我有一个很大的嵌套 div 列表,如下所示:
<div id="ptosTable">
<div class="elbox hue_vals">
<a class="symtype1" href="#">
<div class="sksym">E</div>
</a>
</div>
<div class="elbox hue_domain">
<a class="symtype1" href="#">
<div class="sksym">L</div>
</a>
</div>
<!-- ...plus 100 more just like these -->
</div>
还有一个现有脚本将每个 div (.elbox) 克隆到完全不同的 div (#pinsets )悬停时。 [见下文。]
现在,对于每个克隆,我还想向克隆 div 添加一个 css 类(仅),依赖于源 div 中的类变量。
示例
如果源 div 具有类 hue_vals,则应将类 symhue_vals 添加到 .sksym > 克隆的子 div(仅)。
其他八个变体也是如此:hue_prod 到 symhue_prod、hue_found 到 symhue_found 等。
以下脚本几乎对 hue_vals 执行此操作 ---只不过它似乎在所有 div 中查找 hue_vals 类,而不仅仅是正在克隆的 div
$('.elbox').hover(function() {
$(this).clone().appendTo('#pinsets');
// next three lines add the new class
$(this).each(function(i) {
if($('.hue_vals')){
$('#pinsets .sksym').addClass('symhue_vals');
}
});
$('#pinsets').show();
}, function() {
$('#pinsets').hide();
$('#pinsets').html('');
});
结果是 symhue_vals 被添加到每个克隆的 .sksym div,无论源 div 是否有 hue_vals 类。
所以我想我的问题(最后!)是:如何限制脚本在正在克隆的 div 中查找 hue_whatever _only_ ?
谢谢你!
This question is simple, but the set-up's a bit complex. Please bear with me...
I have a big list of nested divs, like so:
<div id="ptosTable">
<div class="elbox hue_vals">
<a class="symtype1" href="#">
<div class="sksym">E</div>
</a>
</div>
<div class="elbox hue_domain">
<a class="symtype1" href="#">
<div class="sksym">L</div>
</a>
</div>
<!-- ...plus 100 more just like these -->
</div>
There's also an existing script that clones each div (.elbox) to an entirely different div (#pinsets) on hover. [See below.]
Now, for each clone, I also want to add a css class to the clone div (only), dependent on a class variable in the source div.
Examples
If a source div has the class hue_vals, the class symhue_vals should be added to the .sksym subdiv of the clone (only).
Same for eight other variants: hue_prod to symhue_prod, hue_found to symhue_found, etc.
The following script almost does this for hue_vals---except that it seems to look for the hue_vals class in all divs, not just the one being cloned:
$('.elbox').hover(function() {
$(this).clone().appendTo('#pinsets');
// next three lines add the new class
$(this).each(function(i) {
if($('.hue_vals')){
$('#pinsets .sksym').addClass('symhue_vals');
}
});
$('#pinsets').show();
}, function() {
$('#pinsets').hide();
$('#pinsets').html('');
});
The result is that symhue_vals is added to every clone's .sksym div, whether the source div has a hue_vals class or not.
So I guess my question (at last!) is: how can I limit the script to looking for hue_whatever _only_ in the div that's being cloned?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
Try this