简体   繁体   English

jQuery-添加的类没有使用click函数响应

[英]jquery - added class doesn't respond with the click function

Best, 最好,

Can someone help me to find out, how an element with an added class can operate like a element which had the class from in the beginning? 有人可以帮助我找出具有添加的类的元素如何像开始时具有该类的元素一样进行操作吗? --> ($(document).ready(...)) . ->($(文档).ready(...)) I think that the class isn't added to click event, but have no idea how to solve... 我认为该类未添加到click事件中,但不知道如何解决...

<script type="text/javascript">
  $(document).ready(function() {
     ...
      $('span.del').click(function() {
        var id = $(this).parent().attr('id')
        $("#"+id).remove()
        var input_values  = new Array();
        input_values [0]= id;
        load_page("change","taskMenu2.php",input_values,'del')
            })
     ...
  })
</script>

- -

<ul>
    <li id="1" ><span class="del"></span> old </li>
    <li id="2" ><span class="del"></span> old </li>
    <li id="3" ><span class="del"></span> old </li>
    <li id="4" > NEED A DEL SPAN </li>
</ul>

Now : I want to add the span with class "del" to li with the ID 4 . 现在:我要向ID为4的 li添加类“ del”的跨度。 When i click on it (the span), it should execute the function above like the other ones. 当我单击它(跨度)时,它应该像其他函数一样执行上面的功能。

<script type="text/javascript">
   ...
   $("#4").prepend('<span class="del"></span>'); 
   ...
</script>

(jquery 2.1.0 min is used) (使用jquery 2.1.0分钟)

Hoping for a solution 希望找到解决方案

Kind regards 亲切的问候

You can try the .on() function on a container element. 您可以在容器元素上尝试.on()函数。

For example, in this case, something like this: 例如,在这种情况下,如下所示:

$("ul").on("click", ".del", function() { // Your code });

You need to use event delegation here since your span has been dynamically created: 由于您的span已动态创建,因此您需要在此处使用事件委托

$(document).ready(function() {
    $('#4').on('click', 'span.del', function() {
        var id = $(this).parent().attr('id')
        $("#"+id).remove()
        var input_values  = new Array();
        input_values [0]= id;
        load_page("change","taskMenu2.php",input_values,'del')
    });
})

Also, a side note for the id of your list items here. 另外,此处还列出了您的列表项id的旁注。 From the HTML 4 specification : 根据HTML 4规范

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). ID和NAME令牌必须以字母([A-Za-z])开头,后跟任意数量的字母,数字([0-9]),连字符(“-”),下划线(“ _”) ,冒号(“:”)和句点(“。”)。

HTML 5 specification is even more permissive, saying only that: HTML 5规范甚至更宽容,只说:

The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. 该值在元素的主子树中的所有ID中必须唯一,并且必须至少包含一个字符。 The value must not contain any space characters. 该值不得包含任何空格字符。

So for a valid HTML markup, you can change <li id="1" > , <li id="2" > .... to something else such as <li id="li1" > , <li id="li2" >... 因此,对于有效的HTML标记,您可以将<li id="1" ><li id="2" > ....更改为诸如<li id="li1" ><li id="li2" >...

.click() event will not bind event to dynamic html elements. .click()事件不会将事件绑定到动态html元素。 You should use .on() for binding event for dynamic html elements: 您应该使用.on()绑定动态html元素的事件:

$('ul').on('click', 'span.del', function() {
    // YOUR CODE HERE
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM