简体   繁体   English

将jQuery元素追加到包含html的字符串中

[英]Append a jQuery element to a string that contains html

I have a jQuery wrapped element which I would like to append to a html row. 我有一个jQuery包装的元素,我想附加到html行。 I can't wrap my head around this, since append() seemingly accepts strings but not existing jQuery elements (I might be mistaken here). 我无法解决这个问题,因为append()似乎接受字符串,但不接受现有的jQuery元素(在这里我可能会误会)。 I have a following setup: 我有以下设置:

var row='<tr><td>data1</td><td>data2</td><td>';
var img=$('<img src="path/to/img.png"');
img.click(myClickHandler);

Now what I'm trying to do is to append this img element to my row and 'close' the row with a closing tag. 现在,我想做的就是将此img元素附加到我的行中,并用一个结束标记“关闭”该行。 I'm doing it as follows: 我这样做如下:

var jRow=$(row);
jRow.append(img);
jRow.append('</td></tr>');

After my row is ready I append it to my table: 行准备好后,将其追加到表中:

$('#tableId').append(jRow);

Well, all above doesn't work, because I get [Object Object] instead of image tag in my added row. 好吧,以上所有方法都不起作用,因为在添加的行中我得到了[Object Object]而不是image标签。

My goal is to have a row with an image in last cell and a working click handler. 我的目标是在最后一个单元格中有一行图像,并有一个有效的单击处理程序。

Pleease, help. 请帮助。

When you pass a string to append() it is first "converted" to a DOM element/collection. 当您将字符串传递给append()它首先被“转换”为DOM元素/集合。 So, every single string you pass to append() must be valid HTML/XHTML; 因此,传递给append()每个字符串都必须是有效的HTML / XHTML。 you can't add bits of string on later. 您以后不能再添加一些字符串。 The image can still be appended to the table row even if you close the tags beforehand. 即使您事先关闭标签,图像仍可以追加到表格行中。 Eg 例如

var row='<tr><td>data1</td><td>data2</td><td></td></tr>';
var img=$('<img src="path/to/img.png"/>');
img.click(myClickHandler);

var jRow = $(row);
$('td:last', jRow).append(img);

When you pass anything to append() or html() or prepend() (or any other similar method) try to forget about strings; 当您将任何内容传递给append()html()prepend() (或任何其他类似方法)时,请不要理会字符串。 what you just passed is no longer a string; 您刚刚传递的内容不再是字符串; it has been parsed as HTML and has been added to the document as a DOM element (or a number of DOM elements). 它已被解析为HTML,并已作为DOM元素(或许多DOM元素)添加到文档中。

I am not 100% sure, but I think HTML fragments should always be "complete", meaning that adding just "</td></tr>" will not work. 我不确定100%,但是我认为HTML片段应始终是“完整的”,这意味着仅添加"</td></tr>"将不起作用。

A solution would be to build a string with a complete HTML fragment, and then append it, instead of appending the pieces one at a time. 一种解决方案是用完整的HTML片段构建一个字符串,然后附加它,而不是一次附加一个片断。 You can always add the click handler after you created your jQuery object, like this: 创建jQuery对象后,您始终可以添加点击处理程序,如下所示:

jRow.find("img").click(function () { ... });

How about trying with jRow.html() , like this? 这样尝试jRow.html()怎么样?

$('#tableId').append(jRow.html());

It should return you the actual HTML contents instead of the jQuery-wrapped element (jQuery object), which is probably what causes the problem, since append() expects to get a String to append. 它应该返回实际的HTML内容,而不是返回jQuery包装的元素(jQuery对象),这可能是导致问题的原因,因为append()期望获得要追加的String。

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

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