【发布时间】:2010-11-10 21:14:02
【问题描述】:
您好,我有一个关于简单 Javascript 函数的问题。下面是很容易看到的代码,当用户单击单击按钮时,我想切换类我的意思是当页面加载时需要显示“This is para”,然后当用户单击单击按钮时,文本需要更改为这是更新的。但不知道为什么它不起作用。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
.none {
display: none;
}
.display1 {
display: block;
}
</style>
</head>
<body>
<p id="default">This is para</p>
<p id="updated" style="display:none">This is updated</p>
<p><input type="button" value="Click" id="button1" /></p>
<script type="text/javascript">
$(document).ready(function(){
$("#button1").click(function(){
$("#default").addClass("none");
$("#updated").addClass("display1");
});
});
</script>
</body>
</html>
【问题讨论】:
-
为什么不直接使用 jQuery 的 show() 和 hide() 函数?
-
我可以,但只是想澄清一下我对 jquery.addclass 的概念。
-
@add class 和 remove class 主要用于在运行时应用 css 属性,用于显示和关闭你应该像其他人提到的那样使用
-
@gov 我个人不同意,主要是因为它更具侵入性。操作元素类将外观决定排除在代码和它们所属的 CSS 之外。代码做出决策并更新 DOM 的逻辑 状态。
-
另外@Jay,我可以建议你为你的问题想一个更好的标题吗? “javascript 问题”有点笼统,你不觉得吗?
标签: javascript jquery html css