Get content of span

后端 未结 5 1456
北恋
北恋 2020-12-19 05:08

how can I get the contents of span ? I\'m looking for a way for all of this to be vanilla, not jQuery

javascript (and a little jQuery)

var swear_word         


        
5条回答
  •  太阳男子
    2020-12-19 05:26

    There is an issue here:

    var text = document.getElementById('myInput');
    text.text();
    

    You never assigned the text of the input to any variable.

    Following your pattern above, you could do:

    var txt = document.getElementById('myInput'),
        txt = text.text();
    

    The second variable updates the previous variable 'txt' to hold the text of the original 'txt' variable, which was a selector.

    You could do this as well (vanilla javascript, jsfiddle):

    var txt = document.getElementById('myInput').innerHTML;
    
    //or
    
    var txt = document.getElementById('myInput').textContent;
    

提交回复
热议问题