jQuery jquery如何获取标题属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8016072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 09:53:39  来源:igfitidea点击:

jquery how to get the title attr?

jquery

提问by Patrioticcow

i have a simple example:

我有一个简单的例子:

<a id="my_videos" href="#" title="123"><img src="http://placehold.it/350x150"></a>
<a id="my_videos" href="#" title="223"><img src="http://placehold.it/350x150"></a>
<a id="my_videos" href="#" title="323"><img src="http://placehold.it/350x150"></a>

$('#my_videos').live('click', function() {
    var ide = $('#my_videos').attr('title');
    alert (ide);
});

what happens is that every time i click on the link the same value pops up 123

发生的事情是每次我点击链接时都会弹出相同的值 123

what am i doing wrong?

我究竟做错了什么?

thanks

谢谢

here is a jsfiddle

这是一个jsfiddle

回答by Rob W

Rename id="my_videos"to class="my_videos". IDs should be unique. Then, use thisinside the event listener, to refer to the just-clicked element.

重命名id="my_videos"class="my_videos". ID 应该是唯一的。然后,this在事件侦听器内部使用,以引用刚刚单击的元素。

<a class="my_videos" href="#" title="123"><img src="http://placehold.it/350x150"></a>
<a class="my_videos" href="#" title="223"><img src="http://placehold.it/350x150"></a>
<a class="my_videos" href="#" title="323"><img src="http://placehold.it/350x150"></a>

$('.my_videos').live('click', function() {
    var ide = this.title;
    alert (ide);
});

For this case, Vanilla JavaScript is more clear. If you want to use jQuery to get the title, use:

对于这种情况,Vanilla JavaScript 更加清晰。如果要使用 jQuery 获取标题,请使用:

var ide = $(this).attr('title');

回答by Madara's Ghost

The problem in your case is the use of multiple instances of the same ID.

您的情况的问题是使用了同一 ID 的多个实例。

ID by definition can only occur once on the entire page. No 2 elements may have the same ID.

ID 根据定义只能在整个页面上出现一次。没有 2 个元素可以具有相同的 ID。

That is why when jQuery searches for the element you clicked, it only finds the first element with that ID. You'll have to use a class nameinstead.

这就是为什么当 jQuery 搜索您单击的元素时,它只会找到具有该 ID 的第一个元素。您必须改用类名

Also, you should use $(this)to refer to the element that triggered the event (the one you clicked) and not some random element that matches the selector.

此外,您应该使用$(this)来引用触发事件的元素(您单击的元素),而不是一些与选择器匹配的随机元素。

$('.my_videos').live('click', function() { //Note the classname use and not id
    var ide = $(this).attr('title');
    alert(ide);
});

Or an even shorter version using JavaScript's native this:

或者使用 JavaScript 的 native 的更短版本this

$('.my_videos').live('click', function() {
    var ide = this.title;
    alert(ide);
});

回答by Lycha

You should have thisinstead of #my_videos:

你应该有this而不是#my_videos

$('#my_videos').live('click', function() {
    var ide = $(this).attr('title');
    alert (ide);
});