简体   繁体   English

jQuery的未定义的错误获取元素的属性

[英]Jquery undefined error for getting attribute of an element

I have the following code: 我有以下代码:

 <a id="1" href"javascript:void(0)" onclick="myfunc();" class="a">click</a>


 myfunc(){
    var $id=$(this).attr('id');
    console.log($id);

  }

I am getting undefined error ... and can't figure it out. 我收到未定义的错误...并且无法弄清楚。 can any one tell me what is happing? 有人可以告诉我什么吗?

*.js * .js

$(document).on("click",".a",function(){
console.log($(this).attr("id")); 
});

Using jquery you can handle when the element is clicked and get any attribute with $(this).attr("attribute") 使用jquery可以处理单击元素的时间,并可以通过$(this).attr("attribute")获得任何属性

*.html * .html

<a id="1" href"javascript:void(0)" class="a">click</a>

I'm going to assume the following code which fixes your syntax. 我将假设以下代码可修复您的语法。 Please let me know if I am wrong assuming this: 假设这样做有误,请告诉我:

<a id="1" href="javascript:void(0)" onclick="myfunc()" class="a">click</a>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function myfunc(){
    var $id=$(this).attr('id');
    console.log($id);
}
</script>

Your function is actually simply calling myfunc in the global context and the this keyword is referencing window rather than the <a> element. 您的函数实际上只是在全局上下文中调用myfunc,而this关键字引用的是window而不是<a>元素。 To fix this, either use onclick="myfunc.call(this)" . 要解决此问题,请使用onclick="myfunc.call(this)" The complete code is: 完整的代码是:

<a id="1" href="javascript:void(0)" onclick="myfunc.call(this)" class="a">click</a>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function myfunc(){
    var $id=$(this).attr('id');
    console.log($id);
}
</script>
<a id="1" href="javascript:void(0)" onclick="myfunc()" class="a">click</a>

 myfunc(){
    var $id=$(this).attr('id');
    console.log($id);
  }

you were missing a ton of "" and = and stuff 您缺少大量的""=东西

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

相关问题 jQuery获取返回未定义的数据属性 - JQuery getting a data attribute returning undefined 在jQuery库中获取未定义的错误 - Getting undefined error in jquery gallery jquery-单击时获取元素的属性。 元素包含内部div,因此会出现不确定的错误 - jquery - get attributes of an element when clicked . The element contains inner divs so getting undefined error jQuery解析json并获取元素,但未定义 - jQuery parse json and get element, getting undefined 使用jQuery .attr()方法获取元素属性 - Getting element attribute with jQuery .attr() method 将data属性与jquery一起使用时发生未定义的错误 - Undefined error when using data attribute with jquery Jquery获取元素属性的值,并查看它是否标记了不同属性的值 - Jquery getting the value of an element attribute and seeing if it equeals the value of a different attribute 获取循环中最后一个元素的错误“未定义的&#39;getAttribute&#39;” - Getting error “ 'getAttribute' of undefined ” for the last element in the loop jQuery-名称属性中的方括号出现错误 - jQuery - getting error for square brackets in name attribute 在元素上设置属性会给出错误“无法通过Polymer设置属性&#39;...&#39;的undefined” - Setting attribute on element gives error "Cannot set property '…' of undefined' with Polymer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM