[英]How to select certain class in jQuery
Here's the problem; 这是问题所在; I have a couple of divs like the one below, same class name, no id. 我有几个像下面这样的div,相同的类名,没有id。 I need to modify the <span>
text below the <h4>
tag, based on where's the mouse cursor on those 3 images. 我需要根据这3张图片上的鼠标光标在<h4>
标签下面修改<span>
文本。 I do this using javascript, by using mouseenter()
method. 我使用javascript,通过使用mouseenter()
方法来做到这一点。 The problem is that the method changes every span text from whole web page, not only from the class with class name "parent" where the mouse cursor is at the moment. 问题在于该方法更改整个网页中的每个跨度文本,而不仅是更改当前鼠标光标所在的类名称为“ parent”的类。
<div class="parent">
<div class="parent.child">
<a href="#"></a>
<div class="parent.chil.child">
<div class="parent.chil.child.child">
<img src ="link1" data-price="a">
<img src ="link2" data-price="b">
<img src ="link3" data-price="c">
</div>
</div>
</div>
<h4>
<a href= "aspPage.aspx">text</a>
</h4>
<p><span class = "spanClassName">text to be changed</span>some text</p>
<div class=child1"></div>
</div>
How do I select only the link where's the mouse, from the curent "parent" div, even if there are several div with same class name, "parent". 我如何从当前的“父级” div中仅选择鼠标所在的链接,即使有多个具有相同类名“父级”的div也是如此。
I hope I was understood, if not, please ask and I try to explain more. 希望我能理解,如果不明白,请提出,我尝试解释更多。
You can use .closest() to find parent with .parent
class 您可以使用.closest()查找具有.parent
类的父级
$('.parent\\.chil\\.child\\.child img').on('hover', function(){
$(this).closest('.parent').find('.spanClassName').text($(this).attr('data-price'))
});
Additionally as per documents you need to escape .
另外,根据文档,您需要转义.
in your selectors 在您的选择器中
To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\\\. For example, an element with id="foo.bar", can use the selector $("#foo\\\\.bar"). 要将任何元字符(例如!“#$%&'()* +,。/ :; <=>?@ [] ^`{|}〜)用作名称的文字部分,必须使用两个反斜杠进行转义:\\\\。例如,具有id =“ foo.bar”的元素可以使用选择器$(“#foo \\\\。bar”)。
If you already know the specific parent node, just use parent.find('.spanClassName')
. 如果您已经知道特定的父节点,则只需使用parent.find('.spanClassName')
。 If you don't have the parent node, but you know which link the mouse is on, you can use link.closest('.parent').find('.spanClassName')
. 如果没有父节点,但知道鼠标位于哪个链接上,则可以使用link.closest('.parent').find('.spanClassName')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.