【问题标题】:What's wrong with my code in my website我网站上的代码有什么问题
【发布时间】:2016-08-13 18:38:05
【问题描述】:

我只是一个初学者,所以请放心

while (true){
//hide the paragraph
   $("para").hide()
//show the paragraph slowly
   $("para").show("slow")
}
p{
	font-size:1500%;
	text-align: center;
	margin:0;
}

body{
	background:yellow;
}
<!doctype html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="main.css">
</head>
<body>
	<p id="para">this is disco</p>
</body>
	<script src = "jquery.js"></script>
	<script src = "main.js"></script>
</html>

jquery.js 是我计算机中压缩 jquery 的文件名,当我运行它时页面不显示

【问题讨论】:

  • while (true){ 无限循环...
  • jquery.js 是否与您的 html 文件位于同一文件夹中?如果在加载时收到 404 错误,请检查浏览器的开发者工具。
  • 使用while (true) 循环中的当前代码,它将始终执行并导致无限循环。
  • $("para") 匹配 &lt;para&gt; 元素。你想要的是$("#para") 来匹配元素与id="para"

标签: javascript html css


【解决方案1】:
 while (true) { … }

将永远运行(无限循环)并阻塞 UI,因为 JS 是单线程的。它还会一遍又一遍地隐藏段落。如果你想隐藏一个段落,你应该使用:

$(document).ready(function(){ 
    //hide the paragraph
    $("p#para").hide()
    //show the paragraph slowly
    $("p#para").show("slow");
})

在页面加载后执行所有这些操作。

如果您想让段落闪烁,以便在它出现后隐藏»slow«,您可以在页面加载后使用计时器来做到这一点:

(function blink () {
    $("p#para").hide()
    //show the paragraph slowly
    $("p#para").show("slow");

    setTimeout(blink, 1000);
})();

或 CSS 动画。

如果你想让文字闪烁,你根本不需要使用JS。看看这里:Imitating a blink tag with CSS3 animations

»sliding« 效果是由 jQuery 的 show() 方法引起的,记录在 here

【讨论】:

  • 是的,我希望文本闪烁,但是当我设置计时器时,文本会滑入滑出我该怎么办
【解决方案2】:

必须是$("#para")$("para") 搜索不存在的元素 &lt;para&gt;

【讨论】:

  • 想念房间里的大象,不是吗(无限 while 循环...)
  • 我以为这只是为了测试。
【解决方案3】:

元素 id 是“para”,所以要操作它,选择器应该以“#”开头,即“#para”

$(document).ready(function(){ 
//hide the paragraph
$("#para").hide()
//show the paragraph slowly
$("#para").show("slow")

})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2019-06-06
    • 2015-04-04
    相关资源
    最近更新 更多