简体   繁体   English

一个Jquery单击函数用于乘法div

[英]One Jquery click function for multiply divs

Trying to use one click function for all of my div boxes. 试图为我的所有div框使用一个单击功能。 Did some research but still not able to get it working correctly. 做了一些研究,但仍然无法使其正常工作。 I'm new to programming so I might be going about this the wrong way, here is an example of what I'm trying to do: 我是编程的新手,所以我可能会以错误的方式解决这个问题,这里是我正在尝试做的一个例子:

<div id="test_1">
  <p class="example">display this text 1</p>
</div>

<div id="test_2">
  <p class="example">display this text 2</p>
</div>

<div id="test_3">
  <p class="example">display this text 3</p>
</div>

$('div[id^="test"]').each(function(){
    $(this).click(function(){
        test = $(this).parent().find('.example').attr("p")
        alert(test)
    })
})

I thought this would work but if I click on div test 2 or 3 the alert shows "display this text 1" instead of "display this text 2" or "...3" . 我认为这会有效,但是如果我点击div test 23alert显示"display this text 1"而不是"display this text 2""...3"

Any idea what I'm doing wrong? 知道我做错了什么吗?

Don't need each . 不需要each

Bind click on p of each div 绑定点击每个div p

$('div[id^="test"] p').click(function() {
    alert($(this).text());
});

demo 演示

Bind click on div s. 绑定点击div

$('div[id^="test"]').click(function() {
    alert($('p', this).text());
});

$('p', this).text() is the same of $(this).find('p').text() $('p', this).text()$(this).find('p').text()相同$(this).find('p').text()

Remove .parent() . 删除.parent() Otherwise you're getting all the nested .example elements, and retrieving the .attr('p') of the first one. 否则,您将获得所有嵌套的.example元素,并检索第一个元素的.attr('p')

$('div[id^="test"]').each(function(){
    $(this).click(function(){
        var test = $(this).find('.example').attr("p")
        alert(test)
    })
})

Or shortened like this: 或缩短如下:

$('div[id^="test"]').click(function(){
    var test = $(this).find('.example').attr("p")
    alert(test)
})

Your question shows that you're getting a proper text result, though your code shows .attr("p") , which would not. 您的问题表明您获得了正确的文本结果,但您的代码显示的是.attr("p") ,但不会。 I've left it the way it was to keep the code example the same. 我保持代码示例保持不变的方式。

I would change the divs into a class then select them like so: 我会将div更改为一个类,然后像这样选择它们:

<div class="clickable">
    <p>This is the first one</p>
</div>
<div class="clickable">
    <p>This is the second one</p>
</div>
<div class="clickable">
    <p>This is the third one</p>
</div>

and the JS: 和JS:

$(function() {
    $('.clickable').on('click',function() {
        alert($(this).children(':first').text());
    });
});​

Here's a working example: http://jsfiddle.net/CX555/1/ 这是一个有效的例子: http//jsfiddle.net/CX555/1/

The true (and efficient) way to have one click for all your divs is to use event delegation: 对所有div进行一次单击的真实(有效)方法是使用事件委派:

<div id="wrap">
<div id="test_1">
  <p class="example">display this text 1</p>
</div>
<div id="test_2">
  <p class="example">display this text 2</p>
</div>
<div id="test_3">
  <p class="example">display this text 3</p>
</div>
</div>

script: 脚本:

$('#wrap').on('click','div[id^="test"]',function(){
    test = $(this).find('.example').text();
    alert(test);
});

demo: http://jsfiddle.net/hDX8C/ 演示: http//jsfiddle.net/hDX8C/

The single click is attached to the parent ("wrap"), and detects which child was actually clicked. 单击将附加到父级(“换行”),并检测实际单击了哪个子级。

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

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