Javascript 如何使用 jquery 替换/更改 <h3></h3> 中的标题文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8550251/
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
How Do I Replace/Change The Heading Text Inside <h3></h3>, Using jquery?
提问by James Freeberg
How do I change/replace the <h3>
text: "Featured Offers" using javascript to say "Public Offers" instead?
如何更改/替换<h3>
文本:“Featured Offers” 使用 javascript 来代替“Public Offers”?
</div> <!-- FEATURED OFFERS -->
<div class="panel">
<div class="head">
<h3>Featured Offers</h3>
</div>
<div class="body">
<table>
<thead>
<tr>
回答by Sergio Tulentsev
If you can select it, you can manipulate it.
如果你可以选择它,你就可以操纵它。
Try this:
尝试这个:
$(".head h3").html("your new header");
But as others mentioned, you probably want head
div to have an id.
但正如其他人提到的,您可能希望head
div 有一个 id。
回答by barak ben horin
you don't - not like this. give an id to your tag , lets say it looks like this now :
你不 - 不是这样。给你的标签一个 id,让我们说它现在看起来像这样:
<h3 id="myHeader"></h3>
then set the value like that :
然后设置这样的值:
myHeader.innerText = "public offers";
回答by Jie
$("h3").text("context")
Just use method "text()".
只需使用方法“text()”。
The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.
.text() 方法不能用于表单输入或脚本。要设置或获取 input 或 textarea 元素的文本值,请使用 .val() 方法。要获取脚本元素的值,请使用 .html() 方法。
回答by Chamika Sandamal
try this,
尝试这个,
$(".head h3").html("New header");
or
或者
$(".head h3").text("New header");
remember class selectors returns all the matching elements.
记住类选择器返回所有匹配的元素。
回答by talnicolas
Something like:
就像是:
$(".head h3").html("Public offers");
回答by ishanbakshi
Give an id to h3 like this:
像这样给 h3 一个 id:
<h3 id="headertag">Featured Offers</h3>
and in the javascript function do this :
并在 javascript 函数中执行以下操作:
document.getElementById("headertag").innerHTML = "Public Offers";
回答by Deepak kumar Saini
jQuery(document).ready(function(){
jQuery(".head h3").html('Public Offers');
});
回答by Shades88
You can try:
你可以试试:
var headingDiv = document.getElementById("head");
headingDiv.innerHTML = "<H3>Public Offers</H3>";