使用面向对象的方法设置标签元素的属性 [英] Setting for attribute of a label element with object oriented method

查看:94
本文介绍了使用面向对象的方法设置标签元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标签具有"for"属性,该属性使它们指向某个输入字段.我需要使用JQuery更改此属性的值,以便可以使用:

Labels have a 'for' attribute which makes them point to a certain input field. I need to change the value of this attribute with JQuery so I could use:

$("label").attr("for", "targetName");

但是我还需要设置className,所以我更喜欢使用:

But I also need to set the className, so i'd prefer to use:

$("label").attr({
    for: "targetName",
    className: "something" 
});

您可能已经注意到了这个问题,因为它当然是javascript中的关键字.有人知道我该怎么解决吗?目前,我正在使用第一种方法来设置for,第二种方法来设置其他几个属性,它可以工作,但是并不是很漂亮.

You might allready notice the problem, for is ofcourse a keyword in javascript. Does anybody know how I could solve this? Currently i'm using the first method to set the for and the second to set several other attributes, it works but it's not really pretty.

任何帮助将不胜感激.

推荐答案

您也可以使用htmlFor代替for:

you could also use htmlFor instead of for:

$("label").attr({
    htmlFor: "targetName",
    className: "something" 
});

这篇关于使用面向对象的方法设置标签元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆