【发布时间】:2009-03-13 19:57:13
【问题描述】:
需要:想办法将有效的标签/属性/属性添加到普通的 html 控件中。
我有一些 javascript/jquery 将点击事件添加到将显示或隐藏 div 的链接。这个想法是使用 $(document).ready 和一个匿名方法来创建 onClick 在页面加载时调用的方法。单击时,将显示带有一些文本的 div。这一切都很好,除了我不知道如何设置文本以便可以在页面加载时完成。我想要的是这样的:
<a href="..." class="showItLink" textToShow="This is the text to show">HI</a>
这样我就可以做到:
$(document).ready
(
function()
{
$("..showItLink").click
(
function(event)
{
var containerPosition;
var createdDiv;
//see if the div already exists
createdDiv = $(this).children(".postComment");
if (createdDiv.length == 0)
{
//This is where the attribute is used so that the CreateDiv
//method can take the textToShow and fill the div's innerText
//with it V V V V V V
createdDiv = CreateDiv(this.textToShow, "postComment");
$(this).append(createdDiv);
$(this).children(".postComment").hide();
}
$(createdDiv).toggle();
event.preventDefault();
}
);
}
);
现在除了不是 xhtml 有效(meh)之外,这仅适用于 IE。 Firefox 只是说它不存在。 (this.textToShow) 我可以使用 rel 或 rev 之类的东西,但这看起来很老套。我想知道是否有这样做的有效方法。
以下答案的解决方案
comment = $(".showItLink").attr("comment");
...
createdDiv = CreateDiv(comment, "postComment");
搭配:
<a href="http://www.theironical.com" class="showItLink" comment="hihihi" >HI</a>
【问题讨论】:
标签: javascript jquery dynamic