更改 .click() 上的不同元素

发布于 2024-12-02 09:19:50 字数 1966 浏览 0 评论 0原文

我不确定如何以正确的方式使用 .click。

我查看了手册(http://api.jquery.com/click/),但无法找到我的问题的答案:

我正在使用 JS 生成一个 div 结构,如下所示:

<div>
   <img id='img_1' src="click.gif">
</div>
<div id='content_1'> Text</div>

很多这些块都是使用以下函数生成的。最后,每个图像都会获得一个单击事件来更改相关文本的 css:单击 img_1 更改 content_1 上的 css。 我尝试这个代码(简化版本):

$.each(data, function(selector,content) {
     id=prepare(selector);
     $('#boxDiv').append('    <div>\
                               <img id="img_'+id+'" src="click.gif">\
                             </div>\
                             <div id="content_'+id+'"> Text</div>');
     $('#img_'+id).click(function(a) {
                         $('#content_'+id).css('height','100px');});
});

但是这个代码不能按照我的要求工作。

每个 'img_'+id 元素都会获取他相关的点击事件(到目前为止,所以上帝) 但该函数并没有改变相关'content_'+id的css!最后一个内容元素始终会发生变化。 看起来,.click回调函数在添加点击事件时没有获取id,而是在执行回调函数时获取。此时,id(当然)始终是最后一个元素。

所以问题是,如何将当前(相关)id带入 .click 回调函数中?

//更新:我不确定,在这种情况下使用 live() 是否有帮助:我尝试了这个,但没有成功。 问题不在于缺少点击事件。问题是,每次点击时,都会使用最后一个 id 触发回调函数。 例子。 生成的内容是这样的:

<div>
   <img id='img_1' src="click.gif">
</div>
<div id='content_1'> Text</div>
<div>
   <img id='img_2' src="click.gif">
</div>
<div id='content_2'> Text</div>
<div>
   <img id='img_3' src="click.gif">
</div>
<div id='content_3'> Text</div>

JS代码为img_1绑定了一次点击事件,为img_2绑定了一次点击事件,为img_3绑定了一次点击事件。 我将回调函数的内容更改为:

$('#expand_'+id).live('click',function() {console.info(id);});

所以我看到ID的内容:通过单击img_1img_2 id是3。可能是因为,3是最后一个值每个循环。那么如何在回调中获取相关的id呢?

感谢您提供任何帮助。

I'm not sure, how to use .click in the right way.

I looked at the manual (http://api.jquery.com/click/) but could not find the answer for my problem:

I'm generating an div structure with JS like this:

<div>
   <img id='img_1' src="click.gif">
</div>
<div id='content_1'> Text</div>

Lots of those blocks are generated with the following function. At the end, each image gets an click event to change the css of the related text: click on img_1 changes css on content_1.
I try this code (simplified version):

$.each(data, function(selector,content) {
     id=prepare(selector);
     $('#boxDiv').append('    <div>\
                               <img id="img_'+id+'" src="click.gif">\
                             </div>\
                             <div id="content_'+id+'"> Text</div>');
     $('#img_'+id).click(function(a) {
                         $('#content_'+id).css('height','100px');});
});

But this code does not work as I exacted.

Every 'img_'+id Element gets his related click event (so far, so god)
But the function does not change the css of the related 'content_'+id! All the time, the last content element is changed.
It looks like, that the .click call-back function does not get the idat the time of adding click event, but at the time of execution the callback function. At this time, the id is (of course) always the last element.

So the question is, how to bring the current (related) id inside the .click -callback function?

//Update: I'm not sure, if using live() could help in this case: i tried this, without any success.
The problem is not an missing click event. The Problem is, that at every click, the callback-function is fired by using the last id.
Example.
The generated content looks like this:

<div>
   <img id='img_1' src="click.gif">
</div>
<div id='content_1'> Text</div>
<div>
   <img id='img_2' src="click.gif">
</div>
<div id='content_2'> Text</div>
<div>
   <img id='img_3' src="click.gif">
</div>
<div id='content_3'> Text</div>

The JS code binds one click event to img_1, one click event to img_2 and one click event to img_3.
I changed the content of the callback function to:

$('#expand_'+id).live('click',function() {console.info(id);});

SO i see the content of ID: by clicking img_1 or img_2 id is 3. Probably because, 3 is the last value for the each loop. So how can i get the related id inside the call-back?

Thank you for any kind of help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

歌枕肩 2024-12-09 09:19:50

在您的小提琴中发现了问题(以及您更新的代码):

您忘记放入 var< /code> 在 id 前面,这使其成为全局的。所有事件处理程序都引用相同的id,其值是上次迭代中的值。

将其声明为

var id = ....;

本地化。


也就是说,我将使用 jQuery 创建所有元素:

$.each(data, function(selector,content) {
     var id = selector;

     var $img = $('<span />', {
         id: 'img_' + id,
         text: 'click'
     }).click(function() {
          $('#content_'+id).css('height','100px');
     });

     var $div = $('<div />', {
         id: 'content_' + id,
         text: 'Text'
     });

     $('<div />').append($img).append($div).appendTo('#boxDiv');
});

还有其他方法来查找 #content_X 元素。在您的结构中,此元素始终是图像父级的下一个同级元素。因此,您可以在事件处理程序内搜索具有此 ID 的元素:

$(this).parent().next().css(...);

或者甚至将事件处理程序绑定到父 div,而不是直接绑定到图像。

Found the problem in your fiddle (and your updated code):

You forgot to put varin front of id, which makes it global. All event handlers reference the same id and its value is the one from the last iteration.

Declare it as

var id = ....;

to make it local.


That said, I would use jQuery to create all the elements:

$.each(data, function(selector,content) {
     var id = selector;

     var $img = $('<span />', {
         id: 'img_' + id,
         text: 'click'
     }).click(function() {
          $('#content_'+id).css('height','100px');
     });

     var $div = $('<div />', {
         id: 'content_' + id,
         text: 'Text'
     });

     $('<div />').append($img).append($div).appendTo('#boxDiv');
});

There are also other ways to find the #content_X elements. In your structure, this element is always the next sibling of the parent of the image. So instead searching for the element with this ID, inside the event handler, you could do:

$(this).parent().next().css(...);

Or even bind the event handler to the parent div instead directly to the image.

欢你一世 2024-12-09 09:19:50

单击事件处理程序将有权访问 id (和 content)变量,如在父函数中定义的那样。在 JavaScript 中,内部函数可以访问定义它们的函数的所有变量。

问题很简单,您在几个 jQuery 选择器字符串中缺少 # ID 选择器:

$('#boxDiv').append('...');
//some code removed
$('#content_' + id).css('height', '100px');

The click event handler will have access to the id (and content) variable, as it's defined in the parent function. In JavaScript, inner functions have access to all the variables of the function in which they are defined.

The problem is simply that you are missing the # ID selector from a couple of your jQuery selector string:

$('#boxDiv').append('...');
//some code removed
$('#content_' + id).css('height', '100px');
一桥轻雨一伞开 2024-12-09 09:19:50

您应该使用 .live 回调函数,而不是使用 .click 回调函数。引用 jquery 文档:

“描述:为现在和将来与当前选择器匹配的所有元素附加一个处理程序。”

试试这个:

$.each(data, function(id,content) {
 $('boxDiv').append('    <div>\
                           <img id="img_'+id+'" src="click.gif">\
                         </div>\
                         <div id="content_'+id+'"> Text</div>');
 $('#img_'+id).live('click', function() {
                     $('content_'+id).css('height','100px');});
});

Instead of using the .click call-back function, you should use the .live call-back function. Quoting the jquery docs:

"Description: Attach a handler to the event for all elements which match the current selector, now and in the future."

Try this:

$.each(data, function(id,content) {
 $('boxDiv').append('    <div>\
                           <img id="img_'+id+'" src="click.gif">\
                         </div>\
                         <div id="content_'+id+'"> Text</div>');
 $('#img_'+id).live('click', function() {
                     $('content_'+id).css('height','100px');});
});
简单 2024-12-09 09:19:50

你不是说吗

$.each(data, function(id,content) {
     $('#boxDiv').append('    <div>\
                               <img id="img_'+id+'" src="click.gif">\
                             </div>\
                             <div id="content_'+id+'"> Text</div>');
     $('#img_'+id).click, function(a) {
                         $('#content_'+id).css('height','100px');
     });
});

? ID前少了两个#

Don't you mean

$.each(data, function(id,content) {
     $('#boxDiv').append('    <div>\
                               <img id="img_'+id+'" src="click.gif">\
                             </div>\
                             <div id="content_'+id+'"> Text</div>');
     $('#img_'+id).click, function(a) {
                         $('#content_'+id).css('height','100px');
     });
});

? There were two missing # before ID

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文