[英]Find the index of an array element without using built in indexOf function
[英]Can't find the array index using .indexOf()
我正在編寫一個程序,它從文本框中獲取所有文本,將其解析為一個刪除空格的數組,然后搜索數組中的預期值。
我可以測試它並使程序成功獲取文本框中的文本並將其解析到數組中,但是當我嘗試在數組中找到期望的值時,它會拋出錯誤。 錯誤是“對象不支持此屬性或方法”。
JavaScript的:
function generateOutputfvoc()
{
var inputArr = document.getElementById("inputBox").value.split(/[\s]/);
var nameLoc = inputArr.indexOf("Name");
document.getElementById("contactNameOutput").innerHTML = nameLoc;
}
HTML片段:
<p>Paste Text Here</p>
<textarea rows='8' cols='152' id='inputBox' placeholder="Copy text and paste here" wrap="off"></textarea>
<form><input type='button' onclick='generateOutputfvoc()' value='Generate Output' /></form>
<p>Contact Name: <b id='contactNameOutput'></b></p>
謝謝。
您可以根據以下頁面使用javascript的原型功能添加indexOf(): https : //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.