简体   繁体   English

用jquery改变h1的文本,

[英]Changing text of a h1 with jquery,,

hey guys im trying to change the text of a h1 with jquery. 嘿家伙我试图用jquery改变h1的文本。 i want to change the text every time enter key is pressed in a text area and the values are numeric values like on first enter the text should be 1 and on other it should be 2 and so on.. This is the code which im using... 我希望每次在文本区域中按下输入键时更改文本,并且值是数字值,如首先输入文本应该是1,而另一个应该是2,依此类推..这是我使用的代码...

Jquery jQuery的

 <script>
        $('#_co').bind('keypress', function(e) {  //_co is the Id of Text area 
            var code = e.keyCode || e.which;
            var id = 1;
            if(code == 13) {
                var text = $('#_line').text();
                     $('#_line').text(id);
                     id++;
            }

        });
        </script>

Html HTML

<h1 id="_line">0</h4>

Just move your declaration outside of that function, 只需将您的声明移到该函数之外,

var id = 1;

$('#_co').bind('keypress', function(e) {  //_co is the Id of Text area 
    var code = e.keyCode || e.which;

     if(code == 13) {
      var text = $('#_line').text();
      $('#_line').text(id);
      id++;
     }

});

And as Rohit said in the comment, You are having an invalid markup, Just close the h1 tag properly, 正如Rohit在评论中所说,你的标记无效,只需正确关闭h1标签,

<h1 id="_line">0</h1>

As per your new requirement you can use .append() in your current context. 根据您的新要求,您可以在当前上下文中使用.append()

var id = 1;

$('#_co').bind('keypress', function(e) {  //_co is the Id of Text area 
    var code = e.keyCode || e.which;

     if(code == 13) {
      var text = $('#_line').text();
      $('#_line').append('<br>'  + id);
      id++;
     }

});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM