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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:59:09  来源:igfitidea点击:

How do you write to a span using jQuery?

javascriptjqueryajax

提问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 usercountvariable. 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, countUserswill always be table.length-1.
  • you have a typo : dataSetinstead of dataset. Javascript is case sensitive.
  • you don't need to parse the result of the request
  • you don't need to pass empty data : jQuery.postchecks 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 idto the span, you can easily populate the spanwith the help of idand the function .html().

由于您已将 分配idspan,因此您可以spanid和 函数的帮助下轻松填充.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 dataSetrather than dataset?

回调参数应该是dataSet而不是dataset?