JQuery - 将点击事件添加到LI或SPAN元素?

时间:2022-08-26 09:14:38

I am trying to add a click event to a LI element but it is not firing in the page. My page markup looks like:

我试图将一个click事件添加到LI元素,但它没有在页面中触发。我的页面标记看起来像:

<div id="Navigation">
<ul>
    <li class="navPrevNext inactive">|&lt; First Page</li>
    <li class="navPrevNext inactive">&lt; Prev Page</li>
    <li class="navIndex selected" title="0 ">1</li>
    <li class="navIndex notselected" title="1 ">2</li>
    <li class="navIndex notselected" title="2 ">3</li>
    <li class="navPrevNext active" title="1">Next Page &gt;</li>
    <li class="navPrevNext active" title="2">Last Page &gt;|</li>
</ul>
</div>

And in my JS code I have:

在我的JS代码中,我有:

$("#Navigation li").live('click', function(e) { e.preventDefault; this.blur(); return updateNavigation($(this).attr('title')); });

Any thoughts?

有什么想法吗?

TIA

TIA

4 个解决方案

#1


14  

You sure you have jquery 1.3 included in your code? The following works fine (direct copy and paste from your question) for me when I click on any of the LIs:

你确定你的代码中包含了jquery 1.3吗?当我点击任何LI时,以下工作正常(直接复制并粘贴您的问题):

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function updateNavigation(title) {
    alert(title);
}

$("#Navigation li").live('click', function(e) { 
    e.preventDefault; 
    this.blur(); 
    return updateNavigation($(this).attr('title')); 
});

</script>
</head>
<body>
<div id="Navigation">
<ul>
    <li class="navPrevNext inactive">|&lt; First Page</li>
    <li class="navPrevNext inactive">&lt; Prev Page</li>
    <li class="navIndex selected" title="0 ">1</li>
    <li class="navIndex notselected" title="1 ">2</li>
    <li class="navIndex notselected" title="2 ">3</li>
    <li class="navPrevNext active" title="1">Next Page &gt;</li>
    <li class="navPrevNext active" title="2">Last Page &gt;|</li>
</ul>
</div>
</body>
</html>

Are you sure your updateNavigation function is working right? Maybe it's being triggered but something is wrong within it...

您确定您的updateNavigation功能正常吗?也许它被触发了,但内心有些不对劲......

#2


0  

I did the same thing as @Parrots and it works for me. You might, however, want to change your selector to:

我做了与@Parrots相同的事情,它对我有用。但是,您可能希望将选择器更改为:

$("#Navigation li[title]" ).live('click', function(e) {
     e.preventDefault;
     this.blur();
     return updateNavigation($(this).attr('title'));
});

So that the handler is only applied to elements that have a title attribute.

因此处理程序仅应用于具有title属性的元素。

#3


0  

I had a similar problem where I was using a popup list that the user selected. Basically the user clicks an input box and a ul is revealed.

我遇到了类似的问题,我使用的是用户选择的弹出列表。基本上用户点击输入框并显示ul。

This worked great with a 500ms animation, but when switching to 250ms the scrolling down animation was too fast and apparently the click event never fired on a li.

这对于500ms动画效果很好,但是当切换到250ms时,向下滚动动画太快了,显然点击事件从未在li上发生过。

#4


0  

don´t use span it´s better delegate

不要使用span它是更好的委托

#1


14  

You sure you have jquery 1.3 included in your code? The following works fine (direct copy and paste from your question) for me when I click on any of the LIs:

你确定你的代码中包含了jquery 1.3吗?当我点击任何LI时,以下工作正常(直接复制并粘贴您的问题):

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function updateNavigation(title) {
    alert(title);
}

$("#Navigation li").live('click', function(e) { 
    e.preventDefault; 
    this.blur(); 
    return updateNavigation($(this).attr('title')); 
});

</script>
</head>
<body>
<div id="Navigation">
<ul>
    <li class="navPrevNext inactive">|&lt; First Page</li>
    <li class="navPrevNext inactive">&lt; Prev Page</li>
    <li class="navIndex selected" title="0 ">1</li>
    <li class="navIndex notselected" title="1 ">2</li>
    <li class="navIndex notselected" title="2 ">3</li>
    <li class="navPrevNext active" title="1">Next Page &gt;</li>
    <li class="navPrevNext active" title="2">Last Page &gt;|</li>
</ul>
</div>
</body>
</html>

Are you sure your updateNavigation function is working right? Maybe it's being triggered but something is wrong within it...

您确定您的updateNavigation功能正常吗?也许它被触发了,但内心有些不对劲......

#2


0  

I did the same thing as @Parrots and it works for me. You might, however, want to change your selector to:

我做了与@Parrots相同的事情,它对我有用。但是,您可能希望将选择器更改为:

$("#Navigation li[title]" ).live('click', function(e) {
     e.preventDefault;
     this.blur();
     return updateNavigation($(this).attr('title'));
});

So that the handler is only applied to elements that have a title attribute.

因此处理程序仅应用于具有title属性的元素。

#3


0  

I had a similar problem where I was using a popup list that the user selected. Basically the user clicks an input box and a ul is revealed.

我遇到了类似的问题,我使用的是用户选择的弹出列表。基本上用户点击输入框并显示ul。

This worked great with a 500ms animation, but when switching to 250ms the scrolling down animation was too fast and apparently the click event never fired on a li.

这对于500ms动画效果很好,但是当切换到250ms时,向下滚动动画太快了,显然点击事件从未在li上发生过。

#4


0  

don´t use span it´s better delegate

不要使用span它是更好的委托