Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery(this).attr(';id';)返回未定义的_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery(this).attr(';id';)返回未定义的

Javascript jQuery(this).attr(';id';)返回未定义的,javascript,jquery,html,Javascript,Jquery,Html,单击时,我试图从锚标记获取值/id。代码对我来说似乎很好,但我不知道问题出在哪里;每次它都返回未定义或无 还尝试了jQuery(this.attr(“value”),jQuery(this.val()和jQuery(this).attr(“id”) 我尝试过的代码如下: <html> <head> <meta charset="UTF-8"> <title>Document</title> <script

单击时,我试图从锚标记获取值/id。代码对我来说似乎很好,但我不知道问题出在哪里;每次它都返回未定义或无

还尝试了
jQuery(this.attr(“value”)
jQuery(this.val()
jQuery(this).attr(“id”)

我尝试过的代码如下:

<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

</head>
<body>

    <ul class="switch">
        <li value="1"><a href="#" value="1" id="1">link 1</a></li>
        <li value="2"><a href="#" value="2" id="2">link 2</a></li>
        <li value="3"><a href="#" value="3" id="3">link 3</a></li>
        <li value="4"><a href="#" value="4" id="4">link 4</a></li>
    </ul>

    <div class="clickedId"></div>

    <script>
        jQuery(document).ready(function() {
            jQuery('ul.switch').click('a', function(evt) {
                evt.preventDefault();

                var id = jQuery(this).attr("value");

                jQuery('.clickedId').append(id+'<br>');
            });
        });
    </script>
</body>
</html>

文件
jQuery(文档).ready(函数(){ jQuery('ul.switch')。单击('a',函数(evt){ evt.preventDefault(); var id=jQuery(this.attr(“值”); jQuery('.clickedId').append(id+'
'); }); });
您在
ul
上绑定事件,而不是在锚上。因此,事件处理程序中的
$(this)
引用
ul
,并且
ul
没有
id
,它将返回
未定义的

jQuery('ul.switch a')。单击(函数(evt){
evt.preventDefault();
var id=jQuery(this.attr(“值”);
jQuery('.clickedId').append(id+'
'); });

jQuery(文档).ready(函数(){
jQuery('ul.switch a')。单击(函数(evt){
evt.preventDefault();
var id=jQuery(this.attr('value');
jQuery('.clickedId').append(id+'
'); }); });
试试这个

jQuery(document).ready(function() {
            jQuery('li').click(function(evt) {
                evt.preventDefault();

                var id = jQuery(this).attr("value");

               jQuery('.clickedId').append(id+'<br>');
            });
        });
jQuery(文档).ready(函数(){
jQuery('li')。单击(函数(evt){
evt.preventDefault();
var id=jQuery(this.attr(“值”);
jQuery('.clickedId').append(id+'
'); }); });
您似乎在将
.on()
混合。单击()

.click()
不使用选择器,它将处理程序直接绑定到jQuery对象的元素

.on()
接受选择器,但也接受事件名称作为字符串,如下所示:

jQuery('ul.switch').on('click', 'a', function(evt) {
您正在单击UL非锚定标签,请尝试以下操作:
jQuery(文档).ready(函数(){
jQuery(文档).on('click','ul.switch a',function(evt){
evt.preventDefault();
var id=jQuery(this.attr(“值”);
jQuery('.clickedId').append(id+'
'); }); });
这是正确执行的代码。代码中的问题是,您试图在UL标记内的锚上触发事件


文件
jQuery(文档).ready(函数(){ jQuery('li')。单击('a',函数(evt){ jQuery('.clickedId').append($(this.attr(“value”)+'
); }); });
jQuery('ul.switch')。单击('a',…
这在
上添加了一个点击处理程序。如果点击值
,'a'
将作为
事件。数据
传递到事件对象中。对于,您不能使用事件委派。改为使用。是的@Tushar您完全正确我仅在
ul
上绑定了事件,这就是获得
未定义的原因e>
jQuery('ul.switch').on('click', 'a', function(evt) {
You are clicking on UL not anchor tag So try this:

<script>
    jQuery(document).ready(function() {
        jQuery(document).on('click', 'ul.switch a', function(evt) {
            evt.preventDefault();

            var id = jQuery(this).attr("value");

            jQuery('.clickedId').append(id+'<br>');
        });
    });
</script>