我正在尝试为 id 和 onclick 添加 attr,它可以在 firefox 和 chrome 上工作,但由于某种原因,它不能在 IE 9 上工作。 有什么替代方法或者我的代码有问题吗?
$(".extendedGridView tr td a").each(function (index)
{
if ($(this).html() == "Update")
{
$(this).attr('id', 'insertButton');
$(this).attr('onclick', "return Validate('extendedGridView')");
}
});
最佳答案
不要为此使用 attr。使用
this.id = 'insertButton';
$(this).click(function(){return Validate('extendedGridView')});
另请注意,如果存在回车符、空格或任何标签,则使用 html() 检查文本将不起作用。更喜欢这个测试:
if ($(this).text().trim() == "Update") {
然后,确保每个元素都有不同的 id。在您的代码中尚不清楚您在这一点上是否同意,但由于您使用的是 each
,恐怕情况并非如此。如果您需要的是使元素更容易选择,请使用类:
$(this).addClass('update');
关于javascript - Jquery attr 在 IE 9 上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13307862/