是否可以使用jQuery设置“for”(AssociatedControlID)属性?我正在尝试这样的东西将它设置为标签后出现的下一个控件:
$('label.asssociateClass').each(function()
{
var toAssociate = $(this).next(':input');
$(this).attr("for", toAssociate.attr("id"));
});
问题在于,如果我没有通过AssociatedControlID在服务器上设置它,它就永远不会到达这里,因为在这种情况下它被呈现为span
而不是label
。有没有办法克服这个问题,或者我必须在服务器上做到这一点?
答案 0 :(得分:3)
您可以使用.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/
答案 1 :(得分:0)
你无法改变asp:label的行为,但你可以使用jquery
更改标签$(function () {
var spans = $('span.label').each(function() {
input = $(this).next('input');
$('<label />').html($(this).html()).attr('for', input.attr('id')).insertBefore(input);
$(this).remove();
});
});