我试图在jquery中创建一个函数,该函数将域名分开。
也就是说,如果域名= "google.co.in“或"google.com”,它应该分隔域,并将两个变量存储在jquery中,作为域名= "google“和TLD = "co.in”。我试了一下,这是我的小提琴,这是我的代码
<p id="demo">Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var str = "www.google.co.in";
var res = str.split(".",1);
document.getElementById("demo").innerHTML=res;
}
</script>发布于 2014-02-13 12:21:05
试着像这样:
var res = str.split(".")[1];
var rest = str.split(".")[2] + "." + str.split(".")[3];发布于 2014-02-13 12:25:30
var ls = self.location.hostname.split( "." );
会给你
["www", "google", "at"]
发布于 2014-02-13 12:23:24
给你:http://jsfiddle.net/a66L5/2/
我修改了您的javascript代码以使用来自jQuery的jQuery函数。与执行document.getElementById("demo").innerHTML=res;时一样,它只显示数组的第一个元素:
<p id="demo">Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = "www.google.co.in";
var res = str.split(".");
var html = "";
$.each(res, function(key, value) { html += value + "<br />" });
document.getElementById("demo").innerHTML = html;
}
</script>https://stackoverflow.com/questions/21753998
复制相似问题