javascript 警报个别属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17351513/
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-10-27 07:59:42  来源:igfitidea点击:

alert individual attribute value

javascriptjqueryif-statement

提问by captainrad

I want to identify each element by it's name attribute. Each element has the same class and will ultimately contain differing dynamic information.

我想通过名称属性来标识每个元素。每个元素都有相同的类,最终将包含不同的动态信息。

For example I would want the following code to alert the individual element's name value:

例如,我想要以下代码来提醒单个元素的名称值:

html:

html:

<p class="pexample" name="0">this is p #1</p>
<p class="pexample" name="1">this is p #2</p>
<p class="pexample" name="2">this is p #3</p>

jquery:

查询:

$('p').on('click', function() {
    if ($('p').attr('name') !== undefined) {
        alert($('p').attr('name'));
    }
})

Here is a jsfiddle.. http://jsfiddle.net/XG7nd/1/

这是一个 jsfiddle .. http://jsfiddle.net/XG7nd/1/

This code however only alerts the initial elements name value. Help is greatly appreciated.

但是,此代码仅提醒初始元素名称值。非常感谢帮助。

回答by PSL

This should do:

这应该做:

$('p').on('click', function() {
   var name = $(this).attr('name');// `this` here refers to the current p you clicked on
   if (name ) {
        alert(name); 
    }
})

While doing $('p').attr('name')this will always give you the name of the first item in the collection.

这样做$('p').attr('name')将始终为您提供集合中第一个项目的名称。

Demo

演示

回答by Alessandro Minoccheri

Try this:

试试这个:

$(document).on('click','p', function() {
    alert($(this).attr('name'));
});

DEMO

演示

回答by Jedediah

You want to use $(this)

你想用 $(this)

$('p').on('click', function() {
    if($(this).attr('name') !== 'undefined') {
        alert($(this).attr('name'));
    }
});

回答by ExceptionLimeCat

This is occurring because you are getting the nameattribute for the first <p>on every click. You need to specify the one that the event originated from:

发生这种情况是因为您在每次点击时都会获得第一个name属性<p>。您需要指定事件源自的事件:

$('p').on('click', function() {
if ($(this).attr('name') !== undefined) {
    alert($(this).attr('name'));
}
})

Note, jQuery selectors return an array of matching elements. You must use the thiskeyword to get a handle on the element in the current context.

注意,jQuery 选择器返回匹配元素的数组。您必须使用this关键字来获取当前上下文中元素的句柄。

FIDDLE

小提琴

回答by Jeff Noel

Explanation

解释

You keep looking for the pelement even on click, so it'll select the first one it finds.

p即使在 上click,您也一直在寻找元素,因此它会选择找到的第一个元素。

What your code says:

你的代码说的是:

When pis clicked:

什么时候p点击:

  • Find a pelement and alert its attribute.
  • 找到一个p元素并提醒它的属性。


What you really want:

你真正想要的是:

When pis clicked:

什么时候p点击:

  • alert the clicked element'sattribute
  • 提醒被点击元素的属性


Solution

解决方案

Select the attribute of this, which is the clicked element.

选择 的属性this,即被点击的元素。

JSFiddle

JSFiddle

JavaScript

JavaScript

$('p').on('click', function() {
    if ($(this).attr('name') !== undefined) {
        alert($(this).attr('name'));
    }
})

Read more about the thiskeyword.

阅读有关this关键字的更多信息。