欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

實(shí)例講解JS中pop使用方法

 更新時(shí)間:2019年01月27日 08:43:08   投稿:laozhang  
在本篇文章里我們給大家分享了關(guān)于JS中pop使用方法的實(shí)例內(nèi)容,有興趣的朋友們學(xué)習(xí)下。

pop() 定義和用法

pop() 方法用于刪除并返回?cái)?shù)組的最后一個(gè)元素。

語法

arrayObject.pop()

返回值

arrayObject 的最后一個(gè)元素。

說明

pop() 方法將刪除 arrayObject 的最后一個(gè)元素,把數(shù)組長度減 1,并且返回它刪除的元素的值。如果數(shù)組已經(jīng)為空,則 pop() 不改變數(shù)組,并返回 undefined 值。

實(shí)例

在本例中,我們將創(chuàng)建一個(gè)數(shù)組,然后刪除數(shù)組的最后一個(gè)元素。請(qǐng)注意,這也會(huì)改變數(shù)組的程度:

<script type="text/javascript">

var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr)
document.write("<br />")
document.write(arr.pop())
document.write("<br />")
document.write(arr)
</script>

輸出:

George,John,Thomas
Thomas
George,John

我們首先來看一下pop方法的基本語法

array.pop();

接著我們來看pop方法使用的具體示例

代碼如下

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title></title>
</head>
<body>
<script type="text/javascript">
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
document.write(scripts.join(" <br> "));
document.write("<br>--使用pop()方法后的結(jié)果--<br>");
scripts.pop();
document.write(scripts.join(" <br> "));
</script>
</body>
</html>

瀏覽器上顯示效果如下

最后腳本之家小編再分享一行代碼,獲取當(dāng)前頁面的id

例如:

http://www.dbjr.com.cn/article/888.htm

有時(shí)候我們需要將888這個(gè)id提取出來,那么就可以這么寫

window.location.pathname.split("/").pop().split(".")[0]

針對(duì)頁面不需要再生成頁面,直接獲取,方便吧。

相關(guān)文章

最新評(píng)論