在div中显示Javascript“return”值
我从库中获得了以下脚本。 通常在chrome调试器中,我将使用e164;我将得到值:Object{Country:UK,Code:UK} 如何从html div读取电话号码并在另一个html div中显示返回的结果? 比如说在div中显示Javascript“return”值,javascript,arrays,Javascript,Arrays,我从库中获得了以下脚本。 通常在chrome调试器中,我将使用e164;我将得到值:Object{Country:UK,Code:UK} 如何从html div读取电话号码并在另一个html div中显示返回的结果? 比如说 <div id="phone">44123486789</div> 因为函数查找是在该外部函数的作用域内定义的,所以它需要进入您在那里拥有的匿名函数的内部。外部匿名功能需要在这两个div和电话号码出现后运行-您可能应该等待窗口上的加载事件。??结果
<div id="phone">44123486789</div>
因为函数查找是在该外部函数的作用域内定义的,所以它需要进入您在那里拥有的匿名函数的内部。外部匿名功能需要在这两个div和电话号码出现后运行-您可能应该等待窗口上的加载事件。??结果如何?什么div??div具有id属性?您可以为其分配任何div id;我忘了包含html部分。document.getElementById'yourDivId'。innerHTML='your result here in html format'使用此代码段,我只是在加载页面时收到错误消息。未捕获的引用错误:e164未定义您能显示整个页面吗?您可能在加载e164之前引用了e164;然后接合e164.1查找功能。但它不起作用。9956335486352 document.getElementById'phone'.innerHTML;getElementById'country'.innerHTML;var country=e164。查找“电话”;你的问题中没有包含你所包含的全部功能吗?你需要知道这就是e164的定义。您需要在e164.lookup调用之前运行该代码。
(function() {
var lookup, prefixes = {
"1201": [ "US", "United States" ],
"1202": [ "US", "United States" ],
"1203": [ "US", "United States" ],
"1204": [ "CA", "Canada" ],
"44": [ "GB", "United Kingdom" ],
};
lookup = function(phone) {
if (phone.length) {
var prefix, c = phone.length;
for (c; c >= 0; c=c-1) {
prefix = phone.substring(0, c);
if (prefixes[prefix]) {
return { country: prefixes[prefix][1], code: prefixes[prefix][0] };
}
}
}
};
if (typeof exports !== "undefined"){
exports.lookup = lookup;
}
if (typeof window !== "undefined"){
window.e164 = { lookup : lookup};
}
})();
var phone = document.getElementById('phone').innerHTML;
var country = lookup(phone).country;
document.getElementById('country').innerHTML = country;