0

您好我正在使用以下代码通过 jquery 应用 css 的多个属性。我的代码是

$("div:contains('Awais')").css( {text-decoration : 'underline', cursor : 'pointer'} );

我收到 javascript 错误

missing : after property id
$("div:contains(John)").css( {text-dec...: 'underline', cursor : 'pointer'} ); 

但是当我移除text-decoration财产时,错误消失了。这段代码有什么问题

4

2 回答 2

4

text-decoration是无效的属性名称,除非它作为字符串用引号括起来:

$("div:contains('Awais')").css( {'text-decoration' : 'underline', cursor : 'pointer'} );

对象属性必须用引号括起来,除非它们是有效的 Javascript 标识符。这适用于对象字面量中的声明,也适用于使用点符号进行访问(因此object.text-decoration无效。

于 2011-09-19T10:53:24.850 回答
4

您不能使用 JavaScript 中未引用的连字符来修改text-decorationuse textDecoration

$("div:contains('Awais')").css( {textDecoration : 'underline', cursor : 'pointer'} );

或者引用它:

$("div:contains('Awais')").css( {'text-decoration' : 'underline', cursor : 'pointer'} );
于 2011-09-19T10:53:54.327 回答