使用jquery如何更改h2元素值?
<div id="d">
<div class="imge">
<img src="meter.jpg" width="450" height="350" alt="" />
<h2>1234578 </h2> //I want to change the value of this element
<p><font size="5">kWh</font></p>
</div>
</div>
<script type="text/javascript">
function showValue()
{
$.ajax({
url: 'Data_collector.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
Here I want to update the h2 element value every 5s
//Set output element html
}
});
}
</script>
我是jquery的新手。如何在div中访问类中的头元素?或者我应该直接访问它?
答案 0 :(得分:1)
.replaceWith()方法从DOM中删除内容,并通过一次调用在其位置插入新内容。考虑这个DOM结构:
<div class="container">
<div class="inner first">Hello</div>
<div class="inner second">And</div>
<div class="inner third">Goodbye</div>
</div>
第二个内部<div>
可以替换为指定的HTML:
$( "div.second" ).replaceWith( "<h2>New heading</h2>" );
这导致结构:
<div class="container">
<div class="inner first">Hello</div>
<h2>New heading</h2>
<div class="inner third">Goodbye</div>
</div>