简体   繁体   English

如何在jQuery中选择某些类

[英]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'))
});

DEMO DEMO

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.

相关问题 写一个 jquery 选择器到 select 一个 class 具有一定的模式 - Write a jquery selector to select a class with a certain pattern jQuery-选择ID包含特定字符串且属于特定类的div - jQuery - select divs with IDs that contain a certain string AND are part of a certain class jQuery选择一个具有某个类的div,它没有另一个类 - jQuery select a div with a certain class, that doesn't have another class 如何在jquery中选择具有特定类名和唯一“名称”属性的按钮? - How to select a button which has a certain class name and a unique “name” attribute in jquery? 如何使用javascript / jquery中的某个类在HTML页面上选择所有角度字段? - How do I select all angular fields on an HTML page using a certain class in javascript/jquery? 如何使用 jquery 标记具有相同 class 标记的一组元素中的某些元素 select - How to select certain elements in a group of elements with the same class tag using jquery 如何使用jQuery选择某些文本节点? - How to select certain text nodes using jquery? 如何使用Jquery / Javascript选择特定元素 - How to select a certain element with Jquery/Javascript 如何选择某个类的每个最后一个块? - How to select every last block with a certain class? 如何为特定类别的每个选择调用插件 - how to call plugin for each select with certain class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM