是否可以使用 jQuery 设置“for”(AssociatedControlID) 属性?我正在尝试类似的方法将其设置为标签后出现的下一个控件:
$('label.asssociateClass').each(function()
{
var toAssociate = $(this).next(':input');
$(this).attr("for", toAssociate.attr("id"));
});
问题是,如果我不通过 AssociatedControlID 在服务器上设置它,它永远不会到达这里,因为在这种情况下它会呈现为 span
而不是 label
。有没有办法克服这个问题,或者我必须在服务器上执行此操作?
最佳答案
您可以使用.replaceWith()
用标签替换跨度。像这样:
$('span.asssociateClass').each(function() {
var l = $("<label class='associateClass' />")
.html($(this).html())
.attr('for', $(this).next(":input").attr("id"));
$(this).replaceWith(l);
});
这是一个简单的示例:http://jsfiddle.net/V73WL/
关于Jquery 设置标签的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2596545/