Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jqueryforeach循环?_Javascript_Jquery - Fatal编程技术网

Javascript jqueryforeach循环?

Javascript jqueryforeach循环?,javascript,jquery,Javascript,Jquery,嘿,我该怎么说呢 表中的每个标记,当有人单击它时,我想运行一个函数 比如: ForEachTag.click ( function (e) { } ) 如果您有这样一张桌子: <table id='test'> <tr> <td><a href="#">test</a></td> <td>Hi</td> </tr> <

嘿,我该怎么说呢

表中的每个标记,当有人单击它时,我想运行一个函数

比如:

ForEachTag.click  
(  
    function (e)   
    {  
    }  
) 

如果您有这样一张桌子:

<table id='test'>
<tr>
    <td><a href="#">test</a></td>
    <td>Hi</td>
</tr>
<tr>
    <td><a href="#">test1</a></td>
    <td>Hi</td>
</tr>
</table>
$('a').click(function(e) {
    alert('test!');
    return false;
});
$(selectorForParentElement).click(function (e) {
    var target = $(e.target());
    // Checked if we clicked the "correct" element, else traverse
    // up the DOM tree until we find the parent element we ARE interested in.
    if(!target.is(whatYouAreInterestedIn)) {
        target = target.parent(whatYouAreInterestedIn).eq(0);
    }
    // Your click handler for the target goes here
});
这只是将某些内容绑定到文档中的所有链接。想要更多的控制

$('#test').find('a').click(function(e) {
    alert('test!');
    return false;
});
这实际上是说:“找到元素中id为
test
的所有
元素,并将这个点击处理程序绑定到它”——jQuery非常强大,因为它以这种方式处理对象集

不过,这只是冰山一角!你可以得到更深入的信息。如果只想绑定第一个
中相对于
出现的
元素,该怎么办?没问题:

$('#test').find('td:nth-child(1) a').click(function(e) {
    alert('test');
    return false;
});

尽管jQuery确实有一个
函数,允许您迭代一组元素,但在绑定事件时,您很少需要它。jQuery喜欢集合,如果有意义的话,它会按照您的要求对一组元素执行任何操作。

如果您有这样一个表:

<table id='test'>
<tr>
    <td><a href="#">test</a></td>
    <td>Hi</td>
</tr>
<tr>
    <td><a href="#">test1</a></td>
    <td>Hi</td>
</tr>
</table>
$('a').click(function(e) {
    alert('test!');
    return false;
});
$(selectorForParentElement).click(function (e) {
    var target = $(e.target());
    // Checked if we clicked the "correct" element, else traverse
    // up the DOM tree until we find the parent element we ARE interested in.
    if(!target.is(whatYouAreInterestedIn)) {
        target = target.parent(whatYouAreInterestedIn).eq(0);
    }
    // Your click handler for the target goes here
});
这只是将某些内容绑定到文档中的所有链接。想要更多的控制

$('#test').find('a').click(function(e) {
    alert('test!');
    return false;
});
这实际上是说:“找到元素中id为
test
的所有
元素,并将这个点击处理程序绑定到它”——jQuery非常强大,因为它以这种方式处理对象集

不过,这只是冰山一角!你可以得到更深入的信息。如果只想绑定第一个
中相对于
出现的
元素,该怎么办?没问题:

$('#test').find('td:nth-child(1) a').click(function(e) {
    alert('test');
    return false;
});

尽管jQuery确实有一个
函数,允许您迭代一组元素,但在绑定事件时,您很少需要它。jQuery喜欢集合,如果有意义的话,它会对一组元素做任何你要求它做的事情。

好吧,这取决于你想怎么做。基本上,您有两种选择:

  • 为与选择器匹配的每个标记添加单击侦听器
  • 添加父容器的click侦听器,并找出在click处理程序函数(事件委派)中单击的元素
  • 要将相同的click listener添加到多个标记,请执行以下操作:

    $(selectorForElements).click(function (e) {
        // Click handling code here.
        // e is the event object
        // this inside the handler refers to the element that was clicked.
    });
    
    要使用,请执行以下操作:

    <table id='test'>
    <tr>
        <td><a href="#">test</a></td>
        <td>Hi</td>
    </tr>
    <tr>
        <td><a href="#">test1</a></td>
        <td>Hi</td>
    </tr>
    </table>
    
    $('a').click(function(e) {
        alert('test!');
        return false;
    });
    
    $(selectorForParentElement).click(function (e) {
        var target = $(e.target());
        // Checked if we clicked the "correct" element, else traverse
        // up the DOM tree until we find the parent element we ARE interested in.
        if(!target.is(whatYouAreInterestedIn)) {
            target = target.parent(whatYouAreInterestedIn).eq(0);
        }
        // Your click handler for the target goes here
    });
    
    如果可能,最好使用事件委派,因为它绑定更少的侦听器,从而消耗更少的内存并提高执行速度,尤其是在较旧的浏览器上。您可以在此处阅读一些有关事件委派的信息:


      • 好吧,这取决于你想怎么做。基本上,您有两种选择:

      • 为与选择器匹配的每个标记添加单击侦听器
      • 添加父容器的click侦听器,并找出在click处理程序函数(事件委派)中单击的元素
      • 要将相同的click listener添加到多个标记,请执行以下操作:

        $(selectorForElements).click(function (e) {
            // Click handling code here.
            // e is the event object
            // this inside the handler refers to the element that was clicked.
        });
        
        要使用,请执行以下操作:

        <table id='test'>
        <tr>
            <td><a href="#">test</a></td>
            <td>Hi</td>
        </tr>
        <tr>
            <td><a href="#">test1</a></td>
            <td>Hi</td>
        </tr>
        </table>
        
        $('a').click(function(e) {
            alert('test!');
            return false;
        });
        
        $(selectorForParentElement).click(function (e) {
            var target = $(e.target());
            // Checked if we clicked the "correct" element, else traverse
            // up the DOM tree until we find the parent element we ARE interested in.
            if(!target.is(whatYouAreInterestedIn)) {
                target = target.parent(whatYouAreInterestedIn).eq(0);
            }
            // Your click handler for the target goes here
        });
        
        如果可能,最好使用事件委派,因为它绑定更少的侦听器,从而消耗更少的内存并提高执行速度,尤其是在较旧的浏览器上。您可以在此处阅读一些有关事件委派的信息:


        live()更简洁,并使用事件委派,而不是与target混为一谈……似乎是这样的。。我来自原型公司。从文件上看,我还是不太明白它是如何工作的。。它是否在内部找到包含选择器匹配的所有元素的父级来执行事件委派?或者它总是把自己放在body标签上吗?我没有检查源代码,但它可能会在body标签上,因为它无法知道其他元素可能会出现在什么地方(因为它的广告主要是作为绑定动态添加元素的一种方式)更简洁,使用事件委派…似乎是这样的是。。我来自原型公司。从文件上看,我还是不太明白它是如何工作的。。它是否在内部找到包含选择器匹配的所有元素的父级来执行事件委派?或者它总是把自己放在body标签上吗?我没有检查源代码,但它可能会在body标签上,因为它无法知道其他元素可能会出现在什么地方(因为它的广告主要是作为绑定动态添加元素的一种方式)。$(“#test”)。find(“a”)=$(“#test a”)@Josh:我不太喜欢所有东西都在同一个选择器中,而且我觉得使用find语法会更能表达实际发生的事情。Boo。jQuery选择器引擎的目的是模拟CSS。此外,将另一个调用链接到find函数是低效的(这是不合理的)。我想你只是想在答案中加入一个迷你jQuery教程,我想这很好。实际上,链接另一个调用在大多数情况下效率更高。虽然jQuery允许您将所有内容放在一个选择器中,但它必须通过复杂的解析逻辑来尝试和确定规则。通过将它们分离为函数调用使它们变得越简单,不仅使代码对正在发生的事情更具表现力,而且使jQuery所做的工作更少。$(“#test”)。find(“a”)=$(“#test a”)@Josh:我不太喜欢将所有内容都放在同一个选择器中,另外,我觉得使用find语法更能表达实际发生的事情。jQuery选择器引擎的目的是模拟CSS。此外,将另一个调用链接到find函数是低效的(这是不合理的)。我想你只是想在答案中加入一个迷你jQuery教程,我想这很好。实际上,链接另一个调用在大多数情况下效率更高。虽然jQuery允许您将所有内容放在一个选择器中,但它必须通过复杂的解析逻辑来尝试和确定规则。通过将它们分离为函数调用使它们变得越简单,不仅可以使代码更易于表达正在发生的事情