繁体   English   中英

为什么使用javascript split函数会出现此错误?

[英]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 ---&gt; Carroll County MO. ARES</a>

单击其中之一时,将运行putInDebInput()。 看起来像这样;

function putInDebInput(pidi) {
    alert(pidi); 
    var pidi2 = pidi.split(":")[4]; 
}

警报返回;

https://mystuff.us/indexTest.php#19:2:28:CARROLL

但是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM