什么是 jQuery 中的 Join()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1593770/
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-08-26 11:48:06  来源:igfitidea点击:

What is Join() in jQuery?

jqueryjquery-selectors

提问by mSafdel

What is Join() in jquery? for example:

jquery中的Join()是什么?例如:

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

回答by Greg

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

这不是一个 jQuery 函数——它是常规的Array.join函数。

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

它将数组转换为字符串,将参数放在每个元素之间。

回答by Jan Aagaard

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.

我不知道这有什么实际用途。

回答by Del Pedro

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

这个构造的实际用途?它是字符串上的 javascript replaceAll()。

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

will output:

将输出:

stackoverflow is cool

回答by Chandan Gorapalli

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

join 不是一个 jQuery 函数。它是一个 javascript 函数。

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 (,).

join() 方法将数组的元素连接成字符串,并返回字符串。元素将被指定的分隔符分隔。默认分隔符是逗号 (,)。

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

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

回答by Sodhi saab

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

我使用 join 将数组中的单词与 "and, or , / , &"

EXAMPLE

例子

HTML

HTML

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

JS

JS

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

Results

结果

London or Mexico or Canada

回答by taelor

A practical example using a jQuery example might be

使用 jQuery 示例的实际示例可能是

 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.

我在我正在使用的日历工具中这样做,在页面加载时,我可以用今天的日期做某些事情。