使用jquery设置div标签的值 [英] Use jquery to set value of div tag
问题描述
我定义了这个html div标签:
< div style =height:100px; width:100pxclass = 总标题 >
第一个文本
< / div>
我有jQuery代码来改变它的值:
$('div.total-title')。html('test');
但是这并不会改变div的内容。
$('div.total-title')。text('test');
无论如何,关于您共享的问题,我想您可能会在HTML之前调用JavaScript代码正在将DIV的代码发送到浏览器。确保你在< script>中调用jQuery行
$(document).ready(
)在< div> function(){
$('div.total-title')。text('test');
}
);
脚本在div的HTML被浏览器解析后执行。
I have this html div tag defined:
<div style="height:100px; width:100px" class="total-title">
first text
</div>
I have jquery code to change its value:
$('div.total-title').html('test');
But this does not change the content of the div.
if your value is a pure text (like 'test') you could use the text() method as well. like this:
$('div.total-title').text('test');
anyway, about the problem you are sharing, I think you might be calling the JavaScript code before the HTML code for the DIV is being sent to the browser. make sure you are calling the jQuery line in a <script> tag after the <div>, or in a statement like this:
$(document).ready(
function() {
$('div.total-title').text('test');
}
);
this way the script executes after the HTML of the div is parsed by the browser.
这篇关于使用jquery设置div标签的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!