jQuery 如何设置div值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4251540/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:56:40  来源:igfitidea点击:

How to set div value

jquery

提问by Nick Kahn

How do I set the value/text of a div?

如何设置 a 的值/文本div

<div id="numberOf"></div>

$("#btn").click(function (event) {
  $.getJSON('http://host/myServiceImpl.svc/GetCount?method=?',
    { id '1' },
    function (data) {
      alert(data);
      $("#numberOf").val = data;
    }
  );
});

回答by Dean Taylor

Text: http://api.jquery.com/text/

文字:http: //api.jquery.com/text/

$("#numberOf").text(data);

Html: http://api.jquery.com/html/

html:http: //api.jquery.com/html/

$("#numberOf").html(data);

回答by meder omuraliev

^ Google gives you everything you need. I would suggest googling before asking.

^ Google 为您提供所需的一切。我建议在问之前谷歌搜索。

So to answer, you'd want

所以要回答,你会想要

$('#numberOf').text(data) 

or

或者

$('#numberOf').html(data)

回答by craigmoliver

or

或者

$('#numberOf').html(data);

回答by davidsleeps

Set the text property...

设置文本属性...

$("#btn").click(function (event) {
                $.getJSON('http://host/myServiceImpl.svc/GetCount?method=?', { id '1' },
                function (data) {
                    alert(data);
                    $("#numberOf").text(data);

                });
            });

回答by kishan Radadiya

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

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

 $('#numberOf').text(data); Or $('#numberOf').html(data);

Both are working fine.

两者都工作正常。