gpt4 book ai didi

JavaScript 不工作?

转载 作者:行者123 更新时间:2023-11-28 10:34:29 25 4
gpt4 key购买 nike

由于某种原因这不起作用?我想用一些项目填充 ul#list,但它不起作用。

var list = "";
for (var i = 0 ; i<=history.length; i++) {
list += "<li onclick=\"copyShortURL('"+history[i].shortURL+"');\"><div class=\"short\">"+history[i].shortURL+"</div><div class=\"long\">"+history[i].longURL+"</div></li>";
}
document.getElementById('list').innerHTML = list;

谁能告诉我出了什么问题:(我的意思是它没有做任何事情,没有列表项被放入列表中?

附注这仅适用于 mobilesafari :)

最佳答案

createElement方法document采用单个字符串作为您要创建的元素名称。然后,您可以使用 DOM 方法和属性来分配点击处理程序,然后使用 innerHTML 或 DOM 方法创建内部元素。

您的循环中还有一个错误:您需要 <相反<=检查 history 的长度时数组。

var ele, div, list = document.getElementById("list");

var createClickHandler = function(url) {
return function() {
copyShortURL(url);
};
}

for (var i = 0, len = history.length; i < len; ++i)
{
ele = document.createElement("li");
ele.onclick = createClickHandler(history[i].shortURL);

div = ele.appendChild( document.createElement("div") );
div.className = "short";
div.appendChild( document.createTextNode(history[i].shortURL) );

div = ele.appendChild( document.createElement("div") );
div.className = "long";
div.appendChild( document.createTextNode(history[i].longURL) );

list.appendChild(ele);
}

关于JavaScript 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1988454/

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