简体   繁体   English

获取子元素ID的最简单方法是什么?

[英]What is the easiest way to get the id of a child element?

I have an up-vote button that has an child element with a dynamic id. 我有一个上投票按钮,其中有一个带有动态ID的子元素。 How can I fetch the id of that child element? 如何获取该子元素的ID?

I'm trying to add a voting feature to a blog site, and each blog has a unique ID that I need for the ajax call to log the vote in the database and also to update the vote tally in the html. 我正在尝试向博客网站添加投票功能,并且每个博客都有一个唯一的ID,我需要用它来进行ajax调用以将投票记录到数据库中,并更新html中的投票计数。

 $(".plus").click(function() { var myvar = $(".plus").find("h4"); console.log(myvar); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class='plus'> <h4>up</h4> <h3 id='{{blog.id}}'>0</h3> </button> 

Provided you want to get the id of h3 element and print it out in myvar Check this link 如果您要获取h3元素的ID并在myvar中将其打印出来,请检查此链接

$(".plus").click(function(){
     var myvar = $( ".plus" ).find( "h3" ).attr("id");
     console.log(myvar);
 });

jQuery .children() will be the best option:- jQuery .children()将是最佳选择:-

 $(".plus").click(function(){ var myvar = $( ".plus" ).children( "h3" ).attr('id'); console.log(myvar); var myvar1 = $( ".plus" ).children( "h4" ).attr('id'); console.log(myvar1); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class='plus'> <h4 id="h4_id">up</h4> <h3 id='h3_id'>0</h3> </button> 

Why .children() is best in your case :- https://stackoverflow.com/a/648014/4248328 为什么.children()最适合您的情况.children() : //stackoverflow.com/a/648014/4248328

Although you can use this to get the child element's id: 尽管您可以使用它来获取子元素的ID:

$(".plus").click(function() {
    var myvar = $(this).find("h3")[0].id;
    console.log(myvar);
});

Yet i feel that if you change your markup a little that can also be possible with some data-* attributes: 但是我觉得,如果您稍微更改标记,使用data-*属性也可以实现:

 $('.plus').click(function(){ // jquery version var blogId = $(this).data('blogId'); console.log("jq .data() version::::", blogId); // js version var blgId = this.dataset.blogId; console.log("js .dataset version::::", blgId); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class='plus' data-blog-id='{{blog.id}}'> <h4>up</h4> <h3 id='{{blog.id}}'>0</h3> </button> 

you can use it like this. 您可以像这样使用它。

$(".plus").click(function() {
 var myvar = $(this).find("h3").attr("id");
 alert(myvar);
});

If you have multiple .plus items and want to get the id of each one on click, you can use the this context. 如果您有多个.plus项,并且想要在单击时获取每个项的id ,则可以使用this上下文。

 $(".plus").click(function() { var myvar = $(this).find("h4").text(); var myid = $(this).find("h3").attr("id"); console.log(myvar, myid); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class='plus'> <h4>up</h4> <h3 id='id_1'>0</h3> </button> <button class='plus'> <h4>down</h4> <h3 id='id_2'>0</h3> </button> 

With this code, when you click a .plus button, it will look itself for the value of the <h4> snippet and the id of the <h3> . 使用此代码,当您单击.plus按钮时,它将自动查找<h4>代码段的值和<h3>的ID。

Using a $(".plus") selector instead of this inside the click function will select ALL the buttons in the page when you click one, and the attr() method will return only the first ID instead of the current one. 在单击函数中使用$(".plus")选择器代替this选择器将在单击一个按钮时选择页面中的所有按钮,而attr()方法将仅返回第一个ID,而不返回当前的ID。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM