Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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 如何将JS函数添加到动态创建的div中?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何将JS函数添加到动态创建的div中?

Javascript 如何将JS函数添加到动态创建的div中?,javascript,jquery,html,Javascript,Jquery,Html,当用户按下按钮时,我动态地创建一个表行,其中包含一个div。div具有“contents”类 $(document).on(“单击”、“.vote”,函数()){ //删除所选行 var rowLoc=this.parentNode.parentNode.rowIndex; document.getElementById(“新闻”).deleteRow(rowLoc); //将新行添加到底部。 var table=document.getElementById(“新闻”); //将内容添加到新单

当用户按下按钮时,我动态地创建一个表行,其中包含一个div。div具有“contents”类

$(document).on(“单击”、“.vote”,函数()){
//删除所选行
var rowLoc=this.parentNode.parentNode.rowIndex;
document.getElementById(“新闻”).deleteRow(rowLoc);
//将新行添加到底部。
var table=document.getElementById(“新闻”);
//将内容添加到新单元格。
var titleCell=newRow.insertCell(0);
$(标题单元格).addClass(“文章”);
titleCell.innerHTML=“”+文章[索引].text+”;
索引++;
});
document.getElementById(“内容”).innerHTML=输出;
//使用div class=“contents”向原始行添加shorten函数
$(函数(){
$(“.contents”).shorten();
});

如何将
shorten()
应用于类“contents”的新div?

我会在单击处理程序中执行此操作:

var cell = $(titleCell).addClass("articles");
titleCell.innerHTML = ...
cell.find('.contents').shorten();

innerHTML不更新DOM,因此您可以将.append()与HTML一起使用。

通常div和表之间不能很好地配合。使用jQuery时,使用
$('#content').HTML(output)
而不是
document.getElementById(“content”).innerHTML=output
。这破坏了我的一些其他代码。我有以下内容是否重要:
$.ajaxSetup({async:false})它是如何破坏您的代码的?我不认为你的ajaxSetup是相关的,但也许@mikhailov所说的可能是。也许可以尝试
titleCell.html(…)
而不是
titleCell.innerHTML=…
var cell = $(titleCell).addClass("articles");
titleCell.innerHTML = ...
cell.find('.contents').shorten();