如果包含特定文本,jQuery 将悬停类添加到不同的 div (x100)
出于某种原因,我只是无法掌握如何告诉 jQuery 查看一组中的每个 div,然后根据该 div 的内容做出决定。
我的 HTML 有点像这样:
<div id="maintable">
<div class="elbox">
content
<div class="options">A,B,C</div>
</div>
<div class="elbox">
content
<div class="options">B,F</div>
</div>
<!-- and about a hundred more just like these -->)
</div>
<div id="menutable">
<div class="optionA">A</div>
<div class="optionB">B</div>
<div class="optionC">C</div>
</div>
我想创建一个悬停函数来执行以下操作:
- 将鼠标悬停在 #menutable .optionA 上时,
- 脚本会扫描所有 #maintable .options div 中的文本字符串“A
- 如果在 div 中找到“A”, ”。 options,没有任何反应
- 如果在 div.options 中找不到“A”,脚本会向父 div (.elbox) 添加一些 CSS
- 删除 mouseout 上的新类
- 在悬停 .optionB 时执行类似操作(查找“B”)并.optionC(查找“C”)
到目前为止,我有这个:
$('.menutable').hover(function() {
$('.options').each(function(i) {
if (!$(".options:contains('A')")) {
$('.elbox').addClass('bgtransp');
}
});
});
但它不起作用。
但是,如果我翻转逻辑,以便在找到“A”时发生 addClass,则该类将在悬停时应用于每个 .elbox。所以,我认为我的脚本实际上可能是在说,“如果任何 .options div 有一个“A”,那么隐藏每个 .elbox。
我做错了什么?是我的“each?”我错过了父选择器吗?。 ..或者只是完全错误的方法?
对我的菜鸟表示歉意(我正在努力), 谢谢你!
For some reason, I just can't grasp how to tell jQuery to look at each div in a bunch and then make a decision based on just that div's content.
I have HTML kinda like this:
<div id="maintable">
<div class="elbox">
content
<div class="options">A,B,C</div>
</div>
<div class="elbox">
content
<div class="options">B,F</div>
</div>
<!-- and about a hundred more just like these -->)
</div>
<div id="menutable">
<div class="optionA">A</div>
<div class="optionB">B</div>
<div class="optionC">C</div>
</div>
I want to make a hover function that does this:
- On hover over #menutable .optionA
- Script scans all #maintable .options divs for the text string "A"
- If "A" is found in a div.options, nothing happens
- If no "A" is found in a div.options, script adds some CSS to the parent div (.elbox)
- Remove new class on mouseout
- Do similar on hover of .optionB (look for "B") and .optionC (look for "C")
So far, I have this:
$('.menutable').hover(function() {
$('.options').each(function(i) {
if (!$(".options:contains('A')")) {
$('.elbox').addClass('bgtransp');
}
});
});
But it doesn't work.
However, if I flip the logic so that addClass happens if "A" is found, the class is applied onhover to every .elbox. So, I think my script might actually be saying, "if any .options div has an "A," then hide every .elbox.
What am I doing wrong? Is it my "each?" Am I missing a parent selector? ...Or just the wrong approach entirely?
With apologies for my noobness (I'm working on it),
THANK YOU!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此外,您的选择器正在使用 .当它应该是#时
Also your selector is using a . when it should be a #
这应该可以解决问题:
http://jsfiddle.net/zfPZS/1/
This should do the trick:
http://jsfiddle.net/zfPZS/1/
看这里
http://jsfiddle.net/Kp39C/8/
我想,也许你不想当菜单表悬停时搜索 A,但要从选项内搜索,其中包含当前单元格内的值。
因此,您必须在更原子的元素中应用悬停事件处理程序。
此外,您还误用了“.”。选择器而不是“#”选择器,请查看此页面以获取有关此问题的更多信息:http ://www.w3.org/TR/css3-selectors/
使用正确的选择器,配合 find() 和 has() jquery 函数可以让你的生活变得更轻松:)
see here
http://jsfiddle.net/Kp39C/8/
I think, perhaps you didn't want to search for A when the menutable was hover but to search from within the options, which contained the value that the current cell had inside.
therefore you have to apply the hover event handler in a more atomic element.
Also you were missusing '.' selector instead of "#" selector, take a look at this page for more information on that matter: http://www.w3.org/TR/css3-selectors/
Using the right selector, with the find() and has() jquery functions can make your life way lot easier :)