gpt4 book ai didi

javascript - 当从 css 方法中的属性中删除引号时,jQuery 停止工作

转载 作者:行者123 更新时间:2023-11-30 13:03:56 24 4
gpt4 key购买 nike

在 jQuery 文档中,明确提到引号是可选的,但在我的代码中,当我删除它时,firebug 控制台中的错误标志。

Also, jQuery can equally interpret the CSS and DOM formatting of multiple-word properties. For example, jQuery understands and returns the correct value for both .css({'background-color': '#ffe', 'border-left': '5px solid #ccc'}) and .css({backgroundColor: '#ffe', borderLeft: '5px solid #ccc'}). Notice that with the DOM notation, quotation marks around the property names are optional, but with CSS notation they're required due to the hyphen in the name.

引用链接:http://api.jquery.com/css/#css-propertyName-value

让我知道我在这个概念上做错了什么,以及在 css 方法中使用属性/值对的方法是什么?

完整代码-

<!DOCTYPE html>
<html>
<head>
<title>jQuery CSS check</title>
<style>
li {color:red;}
</style>
</head>
<body>
<ul>
<li>Product 1</li>
<li>Product 2</li>
</ul>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
jQuery(document).ready(function(){
jQuery('li').css({color:'green',font-size:'18px'});
});
</script>
</body>
</html>

Firebug 控制台中的错误:SyntaxError: missing : after property id

最佳答案

雅;引号是可选的,但不适用于包含“-”字符的属性。所以,你必须使用引号来包裹这种属性:

jQuery('li').css({color:'green','font-size':'18px'});

正如 yoshi 指出的那样,您也可以使用驼峰式语法属性:

jQuery('li').css({color:'green',fontSize:'18px'});

关于javascript - 当从 css 方法中的属性中删除引号时,jQuery 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16374007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com