gpt4 book ai didi

javascript - 如何给动态创建的div添加JS函数?

转载 作者:行者123 更新时间:2023-11-28 01:02:09 25 4
gpt4 key购买 nike

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

$(document).on("click", ".vote", function() {
//Deletes selected row
var rowLoc = this.parentNode.parentNode.rowIndex;
document.getElementById("news").deleteRow(rowLoc);

//Adds new row to bottom.
var table = document.getElementById("news");

//Adds content to new cell.
var titleCell = newRow.insertCell(0);
$(titleCell).addClass("articles");
titleCell.innerHTML = "<h3><a href='" + articles[index].url + "'>" + articles[index].title + "</a></h3><div class='contents'>" + articles[index].text + "</div>";
index++;
});

document.getElementById("content").innerHTML=output;

// Adds shorten function to original rows with the div class="contents"
$(function(){
$(".contents").shorten();
});

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

最佳答案

我会在您的点击处理程序中执行此操作:

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

关于javascript - 如何给动态创建的div添加JS函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25494477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com