简体   繁体   English

无法使用jQuery更改跨度文本

[英]Cannot change span text using jquery

I am trying to change the text inside the spans using this piece of jquery but I am unable to. 我正在尝试使用这段jQuery来更改跨度内的文本,但我无法这样做。 Why does this not work? 为什么这不起作用? What is the problem with this piece of code? 这段代码有什么问题?

<html> 
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">< /script>
<script>
$(document).ready(function(){

    $("span").text("Changed");
});
});

</script>
</head>

<body>

<p>This is a <span>section</span>.</p>

 <p>This is <span>another</span> paragraph.</p>

</body>
 <html>

Keep eyes on console for errors .. In your code you got an error Uncaught SyntaxError: Unexpected token }", 密切注意控制台上的错误..在您的代码中,您收到错误Uncaught SyntaxError: Unexpected token }",

$(document).ready(function(){

    $("span").text("Changed");
});   
}); //<<<<<<<<<<<<<< remove this line

Working demo 工作演示

 $(document).ready(function(){ $('span').text('Changed'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>This is a <span>section</span>.</p> <p>This is <span>another</span> paragraph.</p> 

It should work 它应该工作

 $(document).ready(function(){ $("span").text("Changed"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <p>This is a <span>section</span></p> <p>This is <span>another</span> paragraph.</p> 

Just be more mindful: 请多加注意:

1) Space before / in script tag is breaking your code 1)脚本标记之前/中的空格破坏了代码

2) Html should be closed 2)HTML应该关闭

3) }); 3)}); is duplicated for some reason 由于某种原因被复制

And You can use shorter notation: 并且您可以使用较短的符号:

$(function() {}) 

instead of: 代替:

$(document).ready(function() { })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM