function insertChildDiv(parentId)
{
var ChildComment = new Element('div', {'class':'padding'});
ChildComment.update('testing');
$().insert(ChildComment);
}
我想要该行 $() 内的parentId 值
$().insert(ChildComment);
我已经尝试过
$('parentId').insert(ChildComment);
但它将parentId评估为字符串,并且正在寻找元素id为“parentId”的div,因此错误将是$(“parentId”) is null
也尝试过
$(parentId).insert(ChildComment);
但它返回错误“$(parentId).insert不是函数”
顺便说一句,该脚本是由该 html 调用的
<a href="#" onclick="insertChildDiv(123456)" id="123456more">Replies and more</a>
最佳答案
$() 接受字符串或 DOM 元素作为参数
进行编辑以反射(reflect)原始 Q 中的编辑。
只要 id 有效,$(parentId) 就会起作用。
传递给函数的 id 是“123456”,您要查找的 id 是“123456more”。另外,您传递的是一个整数(123456)而不是字符串。无论如何,由于它们不匹配,原型(prototype)就找不到它。 另外,id 不应该以数字开头:IE8 将失败,因为 w3c 规范规定 id 属性不能以数字开头(如果我没记错的话)
尝试将 html 更改为
<a href="#" onclick="insertChildDiv('more123456')" id="more123456">Replies and more</a>
它应该可以工作
关于javascript - 如何在原型(prototype)的 $() 实用方法中传递变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/910680/