使用 Jquery 替换 div 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18024588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Jquery to replace text inside a div
提问by user2597457
I have something that looks like
我有一些看起来像
<div id="ABCLinks"> : <span dir="ltr"><a href="www.google.com" title="GOOGLE">Google</a></span>
I am trying to replace only the text ":" with "Websites:" Without adding any source tags around it ().
我试图只用“网站:”替换文本“:”,而不在它周围添加任何源标签()。
This is what I have tried with no effect:
这是我尝试过但没有效果的方法:
<script type="text/javascript">
$(document).ready(function() {
? $("#ABCLinks").text(function () {
return $(this).text().replace(":", "Websites:");
});?????
});
</script>
I am getting this html output (I am using a php skin to automatically embed my jquery script, it works for other jquery scripts)
我得到这个 html 输出(我使用 php 皮肤自动嵌入我的 jquery 脚本,它适用于其他 jquery 脚本)
<script type="text/javascript">$(document).ready(function() {
​$("#ABCLinks").text(function () {
return $(this).text().replace(":", "Websites:"); });​​​​​
});
</script>
采纳答案by zajd
Two issues,
两个问题,
Your selector is incorrect
?$("#ABCLinks")
is for referencing the ID field.The HTML function would be a lot better for this
您的选择器不正确
?$("#ABCLinks")
用于引用 ID 字段。HTML 函数对此会好很多
http://api.jquery.com/html/#html-htmlString
http://api.jquery.com/html/#html-htmlString
.html( htmlString )
.html( htmlString )
htmlString
html字符串
Type: htmlString
类型:htmlString
A string of HTML to set as the content of each matched element.
要设置为每个匹配元素的内容的 HTML 字符串。
<script type="text/javascript">
$(document).ready(function() {
?$("#ABCLinks").html("Websites:");
});?????
</script>
<script type="text/javascript">
$(document).ready(function() {
?$("#ABCLinks").html("Websites:");
});?????
</script>
edit. And the fiddle for your particular circumstance: http://jsfiddle.net/ERe9z/7/
编辑。以及针对您的特定情况的小提琴:http: //jsfiddle.net/ERe9z/7/
回答by
$(document).ready(function() {
$("#ABCLinks").text(function () {
return $(this).text().replace(":", "Websites:");
});
});
You gave your div an ID, but tried to call it by class in jQuery. Classes are referenced using a period like '.ABCLinks', while IDs are referenced using a hashtag like '#ABClinks'
你给了你的 div 一个 ID,但试图在 jQuery 中按类调用它。类使用“.ABCLinks”等句点引用,而 ID 使用“#ABClinks”等主题标签引用
Here is a fiddle: http://jsfiddle.net/ERe9z/
这是一个小提琴:http: //jsfiddle.net/ERe9z/