JavaScript-a tag Onlick event gets the current Data-name

Source: Internet
Author: User

Reply content:

Example code: http://codepen.io/hj624608494 ...

The problem occurs in this point of this that always points to the object when the function executes.

function choose(){  alert($(this).data('name'));  // undefind 因为choose函数的this指向的是window}function choose2(){  $('#J_a').click(function(){    // 这个点击事件的匿名函数的this 指向的是 $('#J_a') 这个对象    alert($(this).data('name'));  })}choose2()

Bind the event when this is passed in, onclick= "choose (This)"

function choose (x) {

alert(x.getAttribute("data-name"))

}

$ (this). attr (' Data-name ');

Get Property-attr ()
The JQuery attr () method is used to get the property value.

$ (' #toy '). attr (' Data-name ');

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.