简体   繁体   English

使用类名按ID获取元素

[英]Get element by ID using class name

I want get the id of the element that is clicked and within a specified class, and want to print the ID. 我想要获取被单击的元素并在指定类内的ID,并要打印ID。

Here is my JS , I am really new JS , Please help 这是我的JS,我真的是新JS,请帮助

      $('.wrapinner').click(function(e) {
         $('.wrapinner').css("margin-top","0");
         $('.wrapinner').css("opacity","0.4");
         $(this).css("margin-top","25px");
         $(this).css("opacity","1");
         var r= document.getElementById(this).attributes.toString();
        document.write(r);
    });

There are two ways to do this: 有两种方法可以做到这一点:

1) Done without jQuery object (it is faster ) 1)在没有jQuery对象的情况下完成( 速度更快

$(".wrapinner").click(function(){
  //Your Code
  var id = this.id;
  document.write(id);
});

OR 要么

2) Done with jQuery object: 2)使用jQuery对象完成:

$(".wrapinner").click(function(){
  //Your Code
  var id = $(this).attr('id');
  document.write(id);
});

NOTE after jfriend00 's comment. 注: jfriend00的评论。

Use document.write() if really needed and is not harmful to your page OR use console.log() . 如果确实需要使用document.write()且对您的页面无害,请使用console.log() Read Why is document.write considered a "bad practice"? 阅读为什么document.write被认为是“不良做法”?

You can call the native javascript object directly; 您可以直接调用本地javascript对象; this.id . this.id

$('.wrapinner').click(function(e) {
     $('.wrapinner').css("margin-top","0").css("opacity","0.4"); // chained these two
     $(this).css("margin-top","25px").css("opacity","1"); // chained these two
     var r = this.id; // this should already reference the correct element.
    document.write(r);
});

Try this way : 尝试这种方式:

$('.wrapinner').click(function(e) {    
  console.log(this.id);
});

You are not passing id to getElementById, rather you are passing the object it self. 您没有将id传递给getElementById,而是您自己传递了对象。 You can get id of source object of event using this.id not by this . 您可以使用get事件的源对象的ID this.id不是this

Change 更改

var r= document.getElementById(this).attributes.toString();

To

var r= document.getElementById(this.id).attributes.toString();

写吧

document.write(this.id)
$(".wrapinner").click(function(){
  var id = this.id
});

Try this: 尝试这个:

$('.wrapinner').click(function(e) {
         $('.wrapinner').css("margin-top","0");
         $('.wrapinner').css("opacity","0.4");
         $(this).css("margin-top","25px");
         $(this).css("opacity","1");
         var id = $(this).attr('id');
    });

Try this example: 试试这个例子:

<table>
<tbody>
<tr id="CustomerScreen" class="rows"></tr>
<tr id="TraderScreen" class="rows"></tr>
<tr id="DistributorScreen" class="rows"></tr>
</tbody>
</table>
<script>
$('.rows').each(function () {
    var ar = this.id;
    console.log(ar);
});
</script>

You can check this code.. 您可以检查此代码。

$('.wrapinner').click(function(e) {    
var getID = $(this).attr('id');
alert(getID);
});

I am capturing click event on class and then finding its id and class name using parent(). 我正在捕获类的click事件,然后使用parent()查找其ID和类名称。 demo demo 演示演示

$(document).ready(function(){
    $('.class-first ul li, .class-second ul li, .class-third ul li').on('click',function(){
        console.log("child :"+$(this).attr('id') + " Parent:"+$(this).parents().eq(1).attr('class'));
    });
})

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

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