javascript 您如何使用 jQuery 写入跨度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13173336/
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
How do you write to a span using jQuery?
提问by Pomster
I'm trying to populate a <span></span>
element on the page load with jQuery.
我正在尝试<span></span>
使用 jQuery 在页面加载上填充一个元素。
At the moment the value that gets populated into the span is just an integer count.
目前,填充到跨度中的值只是一个整数计数。
Here I have named my span userCount:
在这里,我将我的 span 命名为userCount:
<a href="#" class="">Users<span id = "userCount"></span></a>
I am trying to write the value of the span with no success.
我正在尝试写入跨度的值但没有成功。
$(document).ready(function () {
$.post("Dashboard/UsersGet", {}, function (dataset) {
var obj = jQuery.parseJSON(dataSet);
var table = obj.Table;
var countUsers;
for (var i = 0, len = table.length; i < len; i++) {
var array = table[i];
if (array.Active == 1) {
var name = array.Name;
}
countUsers = i;
}
userCount.innerHTML = countUsers.toString();
});
});
回答by Denys Séguret
You don't have any usercount
variable. Use $(selector)
to build a jquery object on which you can call functions like html.
你没有任何usercount
变量。使用$(selector)
建立一个jQuery对象,你可以调用功能,如HTML。
$('#userCount').html(countUsers);
Note also that
还要注意的是
- you don't need to convert your integer to a string manually.
- if you don't break from the loop,
countUsers
will always betable.length-1
. - you have a typo :
dataSet
instead ofdataset
. Javascript is case sensitive. - you don't need to parse the result of the request
- you don't need to pass empty data :
jQuery.post
checks the type of the provided parameters
- 您不需要手动将整数转换为字符串。
- 如果你不打破循环,
countUsers
将永远是table.length-1
. - 你有一个错字 :
dataSet
而不是dataset
. Javascript 区分大小写。 - 你不需要解析请求的结果
- 您不需要传递空数据:
jQuery.post
检查提供的参数的类型
So, this is probably more what you need, supposing you do other things in the loop :
因此,假设您在循环中执行其他操作,这可能更符合您的需求:
$.post("Dashboard/UsersGet", function (dataset) {
var table = dataset.Table;
var countUsers = table.length; // -1 ?
// for now, the following loop is useless
for (var i=0, i<table.length; i++) { // really no need to optimize away the table.length
var array = table[i];
if (array.Active == 1) { // I hope array isn't an array...
var name = array.Name; // why ? This serves to nothing
}
}
$('#userCount').html(countUsers);
});
回答by Praveen Kumar Purushothaman
Use .html()
!
使用.html()
!
<a href="#" class="">Users<span id = "userCount"></span></a>
Since you have assigned an id
to the span
, you can easily populate the span
with the help of id
and the function .html()
.
由于您已将 分配id
给span
,因此您可以span
在id
和 函数的帮助下轻松填充.html()
。
$("#userCount").html(5000);
Or in your case:
或者在你的情况下:
$("#userCount").html(countUsers.toString());
回答by Sivaram Chintalapudi
Instead of:
代替:
userCount.innerHTML = countUsers.toString();
use:
利用:
$('#userCount').html(countUsers.toString());
回答by Clyde Lobo
Change:
改变:
userCount.innerHTML = countUsers.toString();
to:
到:
$("#userCount").html(countUsers.toString());
回答by Swarne27
You could use
你可以用
$('#userCount').text(countUsers);
to write data to span
将数据写入跨度
回答by yechabbi
The call back argument should be dataSet
rather than dataset
?
回调参数应该是dataSet
而不是dataset
?