简体   繁体   English

如何从一组具有相同类/id 的 javascript 中定位特定元素?

[英]How to target a specific element from a group of them having the same class/id in javascript?

I want to copy the text in the <li> tag using JavaScript.我想使用 JavaScript 复制<li>标签中的文本。 It should be copied when the <li> tag is clicked.当点击<li>标签时,它应该被复制。 All <li> tags have the same class as they will get the same formatting through CSS.所有<li>标签都具有相同的类,因为它们将通过 CSS 获得相同的格式。

As per my research, we need to specify the button a target class which it will copy to clipboard(Using clipboard.js ).根据我的研究,我们需要为按钮指定一个目标class ,它将复制到剪贴板(使用clipboard.js )。 The <li> tag will be generated through js so, to give different id to each one of them will be difficult and will increase the code and reduce the speed too. <li>标签将通过js生成,因此为每个标签指定不同的id会很困难,并且会增加代码并降低速度。

So how can copy the text of the li tag that is being clicked through js/jquery/clipboard.js etc.那么如何通过js/jquery/clipboard.js等复制被点击的li标签的文本呢?


<ul>
    <li class="data">Lorem ipdolor.</li>
    <li class="data">Lo ripsum dolor.</li>
    <li class="data">Lorepsum dor.</li>
</ul>

There are different methods but two that stand up on top.有不同的方法,但有两种方法是最重要的。

 $(function() { $("li[class='data']").click(function(e) { // 1) Use this reference console.log("1: " + $(this).text()); // 2) Use Event Target console.log("2: " + $(e.target).text()); }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul> <li class="data">Lorem ipdolor.</li> <li class="data">Lo ripsum dolor.</li> <li class="data">Lorepsum dor.</li> </ul>

You can put an id on ul:您可以在 ul 上放置一个 id:

<ul id="dataContainer">
        <li class="data">sometext</li>
        <li class="data">sometext1</li>
        <li class="data">sometext2</li>
        <li class="data">sometext3</li>
</ul>

Then:然后:

document.getElementById('dataContainer').addEventListener('click', function(e){
  console.log(e.target.innerText);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定位没有类别或ID的特定元素 - Target a specific element with no class or id 如何在不知道特定元素的类或ID的情况下将其定位为目标,但确实具有该元素的所有DOM详细信息 - How to target a specific element without knowing it's class or id, but do have all DOM details of the element 在Javascript对象数组中,如果该元素本身是一个对象数组,如何通过名称/ ID来定位特定的数组元素? - In a Javascript array of objects, how can I target a specific array element by name/ID if that element is an array of objects itself? 如何从基于 class 的组选择中定位特定的当前活动元素 - How to target a specific, currently active, element out of a group selection based on class 具有相同类别的目标元素 - Target element with same class 如何在JavaScript中定位特定的CSS元素? - how to target specific css element in javascript? 如何使用JavaScript定位特定元素 - How to target a specific element using javascript HTML Helpers从模型属性生成ID。 如果许多元素具有相同的ID,如何使用JavaScript和CSS定位1个特定元素? - Html Helpers generates ID from model properties. How do I target 1 particular element with JavaScript and CSS if many elements have the same ID? 单击时,如何更改没有id / class的特定“ td”元素的背景颜色? - How to change background color of a specific 'td' element having no id/class, when it is clicked? Javascript - 如何使用每个元素的父级 unique_id 定位所有内部 class - Javascript - How do I target all the inner class with it's parent's unique_id for each element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM