使用jquery设置div标签的值。

时间:2021-11-22 20:26:56

I have this html div tag defined:

我定义了这个html div标签:

<div style="height:100px; width:100px" class="total-title">
    first text
</div>

I have jquery code to change its value:

我有jquery代码修改它的值:

 $('div.total-title').html('test');

But this does not change the content of the div.

但这不会改变div的内容。

6 个解决方案

#1


120  

if your value is a pure text (like 'test') you could use the text() method as well. like this:

如果值是纯文本(如“test”),也可以使用text()方法。是这样的:

$('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:

无论如何,关于您正在共享的问题,我认为您可能在将DIV的HTML代码发送到浏览器之前调用JavaScript代码。确保在

后的
$(document).ready(
    function() {
        $('div.total-title').text('test');
    }
);

this way the script executes after the HTML of the div is parsed by the browser.

这样,该脚本在浏览器解析div的HTML之后执行。

#2


9  

To put text, use .text('text')

要放置文本,请使用.text('text')

If you want to use .html(SomeValue), SomeValue should have html tags that can be inside a div it must work too.

如果您想使用.html(SomeValue), SomeValue应该有可以放在div中的html标记,它也必须工作。

Just check your script location, as farzad said.

就像法扎德说的那样,检查一下你的脚本位置。

Reference: .html and text

参考:. html和文本

#3


5  

try this function $('div.total-title').text('test');

试试这个功能(div.total-title)美元。text(测试);

#4


4  

You have referenced the jQuery JS file haven't you? There's no reason why farzad's answer shouldn't work.

您引用了jQuery JS文件,不是吗?法扎德的回答没有理由不奏效。

#5


0  

When using the .html() method, a htmlString must be the parameter. (source) Put your string inside a HTML tag and it should work or use .text() as suggested by farzad.

当使用.html()方法时,html字符串必须是参数。(源代码)将字符串放入HTML标记中,它应该按照farzad的建议使用.text()。

Example:

例子:

<div class="demo-container">
    <div class="demo-box">Demonstration Box</div>
</div>

<script type="text/javascript">
$("div.demo-container").html( "<p>All new content. <em>You bet!</em></p>" );
</script>

#6


0  

use as below:

使用如下:

<div id="getSessionStorage"></div>

< div id = " getSessionStorage " > < / div >

For this to append anything use below code for reference:

为此,请在代码下面添加任何代码以供参考:

$(document).ready(function () {
        var storageVal = sessionStorage.getItem("UserName");
        alert(storageVal);
        $("#getSessionStorage").append(storageVal);
     });

This will appear as below in html (assuming storageVal="Rishabh")

这将在html中如下所示(假设storageVal="Rishabh")

<div id="getSessionStorage">Rishabh</div>

#1


120  

if your value is a pure text (like 'test') you could use the text() method as well. like this:

如果值是纯文本(如“test”),也可以使用text()方法。是这样的:

$('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:

无论如何,关于您正在共享的问题,我认为您可能在将DIV的HTML代码发送到浏览器之前调用JavaScript代码。确保在

后的
$(document).ready(
    function() {
        $('div.total-title').text('test');
    }
);

this way the script executes after the HTML of the div is parsed by the browser.

这样,该脚本在浏览器解析div的HTML之后执行。

#2


9  

To put text, use .text('text')

要放置文本,请使用.text('text')

If you want to use .html(SomeValue), SomeValue should have html tags that can be inside a div it must work too.

如果您想使用.html(SomeValue), SomeValue应该有可以放在div中的html标记,它也必须工作。

Just check your script location, as farzad said.

就像法扎德说的那样,检查一下你的脚本位置。

Reference: .html and text

参考:. html和文本

#3


5  

try this function $('div.total-title').text('test');

试试这个功能(div.total-title)美元。text(测试);

#4


4  

You have referenced the jQuery JS file haven't you? There's no reason why farzad's answer shouldn't work.

您引用了jQuery JS文件,不是吗?法扎德的回答没有理由不奏效。

#5


0  

When using the .html() method, a htmlString must be the parameter. (source) Put your string inside a HTML tag and it should work or use .text() as suggested by farzad.

当使用.html()方法时,html字符串必须是参数。(源代码)将字符串放入HTML标记中,它应该按照farzad的建议使用.text()。

Example:

例子:

<div class="demo-container">
    <div class="demo-box">Demonstration Box</div>
</div>

<script type="text/javascript">
$("div.demo-container").html( "<p>All new content. <em>You bet!</em></p>" );
</script>

#6


0  

use as below:

使用如下:

<div id="getSessionStorage"></div>

< div id = " getSessionStorage " > < / div >

For this to append anything use below code for reference:

为此,请在代码下面添加任何代码以供参考:

$(document).ready(function () {
        var storageVal = sessionStorage.getItem("UserName");
        alert(storageVal);
        $("#getSessionStorage").append(storageVal);
     });

This will appear as below in html (assuming storageVal="Rishabh")

这将在html中如下所示(假设storageVal="Rishabh")

<div id="getSessionStorage">Rishabh</div>