[英]Why this error using the javascript split function?
我使用PHP創建了一系列的href標簽,例如:
echo ("<a href='#$net[id2]' onclick='putInDebInput(this);'>$net[call] ---> $net[org]</a>");
它創建的示例如下所示;
<a href="#3:2:19:CARROLL" onclick="putInDebInput(this);">CARROLL ---> Carroll County MO. ARES</a>
單擊其中之一時,將運行putInDebInput()。 看起來像這樣;
function putInDebInput(pidi) {
alert(pidi);
var pidi2 = pidi.split(":")[4];
}
警報返回;
但是var pidi2 = pidi.split(":")[4];
返回此錯誤;
TypeError: pidi.split is not a function. (In 'pidi.split(":")', 'pidi.split' is undefined)
我沒有在代碼中看到錯誤,所以拆分如何“未定義”?
執行此操作時:
onclick="putInDebInput(this);
您正在調用傳遞HTML元素而不是字符串的putInDebInput
。 我相信您想傳遞href
屬性。
您可以將功能更新為以下形式:
function putInDebInput(pidi) {
var hrefContent = pidi.getAttribute("href");
alert(hrefContent);
var pidi2 = hrefContent.split(":")[3]; // chaged to 3 from 4
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.