【问题标题】:jquery loop through divs and bind eventsjquery 循环遍历 div 和绑定事件
【发布时间】:2010-12-22 09:08:26
【问题描述】:

我有以下 html。你能告诉我如何使用 jquery 循环遍历每个 query_chunk div 并将不同的事件绑定到 div 内的每个元素吗?

我知道我可以使用 .each 函数,但我被语法困住了。任何想法表示赞赏。

                <div id="query_chunk_1">
                    <select class="parameter" id="parameter_1" name="parameter_1">
                        <option title="Keyword Anywhere" value="Anywhere">Keyword Anywhere</option>
                        <option title="Author or Contributor" value="Contributor">Author or Contributor</option>
                        <option title="Title" value="Title">Title</option>
                        <option title="Subject" value="Subject">Subject</option>
                    </select>
                    <input id="keyword_1" name="keyword_1" type="text" />
                    <a href="#"  id="remove_1"  title="Remove">[-]
                        </a>
                </div>
                <div id="query_chunk_2">
                    <select class="parameter" id="parameter_2" name="parameter_2">
                        <option title="Keyword Anywhere" value="Anywhere">Keyword Anywhere</option>
                        <option title="Author or Contributor" value="Contributor">Author or Contributor</option>
                        <option title="Title" value="Title">Title</option>
                        <option title="Subject" value="Subject">Subject</option>
                    </select>
                    <input id="keyword_2" name="keyword_2" type="text" />
                    <a href="#"  id="remove_2"  title="Remove">[-]
                        </a>
                </div>

【问题讨论】:

    标签: jquery events loops html bind


    【解决方案1】:
    关键字无处不在 作者或贡献者 标题 学科 [-]
                <div id="query_chunk_2" class="con">
                    <select class="parameter" id="parameter_2" name="parameter_2">
                        <option title="Keyword Anywhere" value="Anywhere">Keyword Anywhere</option>
                        <option title="Author or Contributor" value="Contributor">Author or Contributor</option>
                        <option title="Title" value="Title">Title</option>
                        <option title="Subject" value="Subject">Subject</option>
                    </select>
                    <input id="keyword_2" name="keyword_2" type="text" />
                    <a href="#"  id="remove_2"  title="Remove">[-]
                        </a>
                </div>
    
    
    $(function() {
         $("div.con").each(function() {
              $(this).live('eventname', functionname);
         });
    });
    

    还请注意,我向 div 添加了类。您还可以使用 .bind 而不是 .live 绑定到事件

    $(this).bind('eventname',function(event){alert('Hi there!');});
    
    $(this).live('eventname',function(event){alert('Hi there!');});
    

    更新 #1

    要绑定到单个控件,请使用:

    $(function() {
             $("div.con").each(function() {
                  $(this).find("elementId").live('eventname', functionname);
             });
        });
    

    【讨论】:

    • 谢谢,但是如何将不同的事件绑定到 div 内的各个控件?例如,我需要将下拉菜单的“更改”事件和“keyup”和“mouseup”事件绑定到文本框?
    • 再次感谢。我没有让这个工作,可能是什么问题? $("div.con").each(function (index) { $(this).find("remove_2").live("click", function () { alert("点击事件触发"); });
    • 你缺少“});”最后。
    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    相关资源
    最近更新 更多