English 中文(简体)
J. 事实将不承认一个既定要素
原标题:JQuery will not recognize a created element
  • 时间:2011-07-05 19:32:23
  •  标签:
  • jquery

我在 trouble忙地认识到一种因形式变化而产生的因素。 当我选择一个链接时,我就这样做了,但当我对所创造的项目加点功能时,不会发生任何事情。 如何做这项工作?

$(function() {

var i = $( #nav li ).size() + 1;

$( a#add ).click(function() {
    $( <li>  + i +  <a href="#" id="add">yes</a></li> ).appendTo( ul );
    i++;
});

$( a#remove ).click(function() {
    $( li:last ).remove();
    i--;
});


});

html code

<ul id="nav">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<a href="#" id="add">Add List Item</a><br/>
<a href="#" id="remove">Remove LI</a>
最佳回答

You need to use live to bind event handlers for dynamically created elements. Your current code will create multiple html elements with same id which is not valid. You can use class names instead to identify the src elements.

为此:

$(function() {
    var i = $( #nav li ).size() + 1;
    $( a.add ).live("click", function() {
        $( <li>  + i +  <a href="#" class="add">yes</a></li> ).appendTo( ul );
        i++;
    });

    $( a.remove ).live("click", function() {
        $( li:last ).remove();
        i--;
    });
});

html Code

<ul id="nav">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
</ul>
<a href="#" class="add">Add List Item</a><br/>
<a href="#" class="remove">Remove LI</a>
问题回答

改变您的格言:

$(function() {
    var i = $( #nav li ).size() + 1;

    $( a#add ).live( click , function() {
        $( <li>  + i +  <a href="#" id="add">yes</a></li> ).appendTo( ul );
        i++;
    });

    $( a#remove ).live( click , function() {
        $( li:last ).remove();
        i--;
    });
});

你可以在这里使用浮标。 .click刚刚与 mo化活动行动相结合。 http://api.jquery.com/live/rel=“nofollow”>。

为了给一名选择人及由酒吧创造的所有其他人举办活动,你必须使用活(现)功能,而不是短手。

您的法典是把点击事件附在你身上的“新”链接。

$( a#add ).live( click , function() {
    $( <li>  + i +  <a href="#" id="add">yes</a></li> ).appendTo( ul );
    i++;
});

id="add" is only allowed on one element per page. If you use it more than once, bizarre things can happen. Change your code to:

$(function() {
    var i = $( #nav li ).size() + 1;

    $( a#add ).click(function() {
        $( <li>  + i +  <a href="#" class="add">yes</a></li> ).appendTo( ul );
        i++;
    });

    $( ul a.add ).live( click , function() {
        // handle any link with class "add" that was dynamically added to the ul
    });

    $( a#remove ).live( click , function() {
        $( li:last ).remove();
        i--;
    });
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签