[英]Using jQuery, how do I change the elements html value? (div)
Using a ajax request I want to change content of my div. 使用ajax请求我想改变我的div的内容。
<div id="d1">202</div>
So I want to change the content to a different number. 所以我想将内容更改为其他数字。
$('d1').InnerText???
Also, say I wanted to increment the number, how could I do that? 另外,说我想增加数字,我怎么能这样做? Do I have to convert to int? 我必须转换为int吗?
$("#di").html('My New Text');
Check out the jQuery documentation . 查看jQuery文档 。
If you wanted to increment the number, you would do 如果你想增加数字,你会这样做
var theint = parseInt($("#di").html(),10)
theint++;
$("#di").html(theint);
PS Not sure if it was a typo or not, but you need to include the #
in your selector to let jQuery know you are looking for an element with an ID of di
. PS不确定它是否是拼写错误,但你需要在你的选择器中包含#
让jQuery知道你正在寻找ID为di
的元素。 Maybe if you come from prototype you do not expect this, just letting you know. 也许如果你来自原型,你不要指望这个,只是让你知道。
这将改变HTML元素的内部文本。
$('#d1').text(parseInt(requestResponse)++);
除非你嵌入html像<b>blah</b>
我建议使用$("#di").text()
因为它会自动转义像<,>和&,而.html()
将不会。
使用文字功能:
$("#d1").text($("#d1").text() + 1);
$('#d1').html("Html here");
if your value is a pure text (like 'test') you could use the text() method as well. 如果您的值是纯文本(如'test'),您也可以使用text()方法。 like this: 像这样:
$('#d1').text('test'); Or $('#d1').html('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. 无论如何,关于你正在分享的问题,我想你可能会在将DIV的HTML代码发送到浏览器之前调用JavaScript代码。 make sure you are calling the jQuery line in a <script> tag after the <div>, or in a statement like this: 确保在<div>之后的<script>标记中调用jQuery行,或者在这样的语句中调用:
$(document).ready(
function() {
$('#d1').text('test');
}
);
this way the script executes after the HTML of the div is parsed by the browser. 这样,在浏览器解析div的HTML之后,脚本就会执行。
$("#div1").innerHTML="your text goes here..";
jQuery('#d1').html("Hello World");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.