What is Join() in jQuery?

后端 未结 6 1378
无人及你
无人及你 2020-12-08 18:14

What is Join() in jquery? for example:

var newText = $(\"p\").text().split(\" \").join(\" \"); 
相关标签:
6条回答
  • 2020-12-08 18:39

    A practical example using a jQuery example might be

     var today = new Date();
     $('#'+[today.getMonth()+1, today.getDate(), today.getFullYear()].join("_")).whatever();
    

    I do that in a calendar tool that I am using, this way on the page load, I can do certain things with today's date.

    0 讨论(0)
  • 2020-12-08 18:43

    The practical use of this construct? It is a javascript replaceAll() on strings.

    var s = 'stackoverflow_is_cool';  
    s = s.split('_').join(' ');  
    console.log(s);
    

    will output:

    stackoverflow is cool
    
    0 讨论(0)
  • 2020-12-08 18:44

    That's not a jQuery function - it's the regular Array.join function.

    It converts an array to a string, putting the argument between each element.

    0 讨论(0)
  • 2020-12-08 18:56

    join is not a jQuery function .Its a javascript function.

    The join() method joins the elements of an array into a string, and returns the string.The elements will be separated by a specified separator. The default separator is comma (,).

    http://www.w3schools.com/jsref/jsref_join.asp

    0 讨论(0)
  • 2020-12-08 18:56

    I use join to separate the word in array with "and, or , / , &"

    EXAMPLE

    HTML

    <p>London Mexico Canada</p>
    <div></div>
    

    JS

     newText = $("p").text().split(" ").join(" or ");
     $('div').text(newText);
    

    Results

    London or Mexico or Canada
    
    0 讨论(0)
  • 2020-12-08 19:01

    You would probably use your example like this

    var newText = "<span>" + $("p").text().split(" ").join("</span> <span>") + "</span>";
    

    This will put span tags around all the words in you paragraphs, turning

    <p>Test is a demo.</p>
    

    into

    <p><span>Test</span> <span>is</span> <span>a</span> <span>demo.</span></p>
    

    I do not know what the practical use of this could be.

    0 讨论(0)
提交回复
热议问题