<script type="text/javascript" src="http://code.jquery.com/jquery-1.12.1.min.js">
$(document).ready(function() {
$("p").click(function() {
alert("button was clicked");
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
});
});
</script>
<body>
<p> This text will change </p>
</body>
点击后为什么这段代码不会改变?
答案 0 :(得分:2)
您不能在脚本中同时拥有这两个代码,并且可以在同一个脚本块上引用脚本。你需要为它们分别制作标签。
<script type="text/javascript" src="http://code.jquery.com/jquery-1.12.1.min.js"></script>
<script>
$(document).ready(function() {
$("p").click(function() {
alert("button was clicked");
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
});
});
</script>
现在代码运行时,单击段落元素时会出现警告。
答案 1 :(得分:0)
如果您想要更改段落中的文本,那么您应该这样做:
$(document).ready(function() {
$("p").click(function() {
alert("button was clicked");
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$("p").text(numbers);
});
});
答案 2 :(得分:0)
您必须使用您定义为variable
p
$(document).ready(function() {
var $numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$("p").click(function() {
$("p").text($numbers)
alert("button was clicked");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>This text will change</p>