无法绑定点击功能

时间:2022-01-23 20:35:36

I found this component made it with jquery, My problem it's that want to bind the click in the elements of the components which are a a element inside the div called menu to get the ID when are clicked. It doesn't work and I don't understand why. Could anybody see the problem??

我发现这个组件是用jquery创建的,我的问题是想要绑定组件元素中的click,这是div中div的一个元素,当点击时获取ID。它不起作用,我不明白为什么。谁能看到问题?

Here's my js

这是我的js

    $(document).ready(initialize);

    function initialize() {

    $('#hierarchybreadcrumb').menu({
        content: $('#menu').html(),
        backLink: false
    });

    $('#menu a').bind('click',obtenerId);   
}

    function obtenerId(){

        alert($(this).attr('id'));
    } 

And here's my live demo

这是我的现场演示

4 个解决方案

#1


2  

Aaaaaand then you look at the console (for your live demo)

Aaaaa然后你看看控制台(为你的现场演示)

无法绑定点击功能

#2


1  

Looking further in your code, none of the <a> tags in the #menu have ids so it alerts undefined. Try giving them ids. That's where I would start.

进一步查看代码,#menu中的标签都没有id,因此它会提示未定义。试着给他们ids。这就是我要开始的地方。

#3


0  

$('a').bind('click',obtenerId);

$( 'A')绑定( '点击',obtenerId);

Mind you I think you want to bind it to the actual specific menu button...

请注意,我认为你想将它绑定到实际的特定菜单按钮......

$('#hierarchybreadcrumb').bind('click', function)

$('#hierarchybreadcrumb')。bind('click',function)

#4


0  

I modified it a bit and got the result I think you want.

我修改了一下,得到了我认为你想要的结果。

$(document).ready(function () {
   $('#menu a').click(obtenerId);   
});

function obtenerId(){
    $("#menuSelection").html($(this).attr('id'));
}

<div style="display:table-row">
    <ul id="menu">
        <li><a href="#" id="1">opcion 1</a></li>
        <li><a href="#" id="2">opcion 2</a></li>
    </ul>
</div>
<div style="display:table-row">
    <p id="menuLog">Elegiste: <span id="menuSelection"></span></p>
 </div>

#1


2  

Aaaaaand then you look at the console (for your live demo)

Aaaaa然后你看看控制台(为你的现场演示)

无法绑定点击功能

#2


1  

Looking further in your code, none of the <a> tags in the #menu have ids so it alerts undefined. Try giving them ids. That's where I would start.

进一步查看代码,#menu中的标签都没有id,因此它会提示未定义。试着给他们ids。这就是我要开始的地方。

#3


0  

$('a').bind('click',obtenerId);

$( 'A')绑定( '点击',obtenerId);

Mind you I think you want to bind it to the actual specific menu button...

请注意,我认为你想将它绑定到实际的特定菜单按钮......

$('#hierarchybreadcrumb').bind('click', function)

$('#hierarchybreadcrumb')。bind('click',function)

#4


0  

I modified it a bit and got the result I think you want.

我修改了一下,得到了我认为你想要的结果。

$(document).ready(function () {
   $('#menu a').click(obtenerId);   
});

function obtenerId(){
    $("#menuSelection").html($(this).attr('id'));
}

<div style="display:table-row">
    <ul id="menu">
        <li><a href="#" id="1">opcion 1</a></li>
        <li><a href="#" id="2">opcion 2</a></li>
    </ul>
</div>
<div style="display:table-row">
    <p id="menuLog">Elegiste: <span id="menuSelection"></span></p>
 </div>