Onclick不适用于以函数为参数的javascript函数?
代码 htmlOnclick不适用于以函数为参数的javascript函数?,javascript,html,Javascript,Html,代码 html <button onclick="getUserIP(function(ip))">Click me</button> <p id="demo"></p> 点击我 脚本 /** * Get the user IP throught the webkitRTCPeerConnection * @param onNewIP {Function} listener function to
<button onclick="getUserIP(function(ip))">Click me</button>
<p id="demo"></p>
点击我
脚本
/**
* Get the user IP throught the webkitRTCPeerConnection
* @param onNewIP {Function} listener function to expose the IP locally
* @return undefined
*/
function getUserIP(onNewIP) { // onNewIp - your listener function for new IPs
//compatibility for firefox and chrome
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({
iceServers: []
}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
function iterateIP(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}
//create a bogus data channel
pc.createDataChannel("");
// create offer and set local description
pc.createOffer(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(iterateIP);
});
pc.setLocalDescription(sdp, noop, noop);
}, noop);
//listen for candidate events
pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
};
}
// Usage
getUserIP(function(ip){
document.getElementById("demo").innerHTML = 'Got your IP ! : ' + ip + " | verify in http://www.whatismypublicip.com/";
});
/**
*通过WebKitRTPeerConnection获取用户IP
*@paramonnewip{Function}侦听器函数在本地公开IP
*@return未定义
*/
函数getUserIP(onNewIP){//onNewIP-新IP的侦听器函数
//firefox和chrome的兼容性
var myPeerConnection=window.rtpeerconnection | | window.mozrtpeerconnection | | window.webkirtpeerconnection;
var pc=新的myPeerConnection({
ICEServer:[]
}),
noop=函数(){},
localIPs={},
ipRegex=/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
钥匙
函数迭代ip(ip){
如果(!localIPs[ip])onNewIP(ip);
localIPs[ip]=真;
}
//创建虚假数据通道
pc.createDataChannel(“”);
//创建报价并设置本地描述
pc.createOffer(功能(sdp){
sdp.sdp.split('\n').forEach(函数(行){
if(line.indexOf('candidate')<0)返回;
line.match(ipRegex).forEach(iterateIP);
});
pc.setLocalDescription(sdp、noop、noop);
},noop);
//倾听候选事件
pc.onicecandidate=函数(ice){
如果(!ice | |!ice.candidate | |!ice.candidate.candidate | |!ice.candidate.candidate.match(ipRegex))返回;
ice.candidate.candidate.match(ipRegex.forEach)(iterateIP);
};
}
//用法
getUserIP(函数(ip){
document.getElementById(“demo”).innerHTML='获得了您的IP!:'+IP+“|在中验证http://www.whatismypublicip.com/";
});
和演示
我正在尝试获取点击此按钮的最终用户的私有ip
当我运行代码时,它不会给我任何输出。基本上,我不知道调用以函数为参数的js函数的语法。谢谢
更新
<button id="a" onclick=(function(ip)
{
document.getElementById("demo").innerHTML = + ip;
})();> Clickme </button>
<p id="demo"></p>
点击我
错误
“未捕获的语法错误:输入意外结束”
您忘记调用getUserIP
函数,还必须将属性值加引号(“…”
)。对于('demo')
部分,您必须使用单引号。试试这个:
//只是一个演示函数(您可以忽略它)
const getUserIP=f=>f(“它工作!”)
点击我
函数(ip)不是有效的函数定义,将抛出语法错误。如果确实要在onclick属性中定义回调,请尝试以下操作:function(ip){/*your code*/}
@subrachnid是正确的,onclick=“getUserIP(function(ip))
没有意义,因为getUserIP
的参数需要是实际的函数。@subrachnid我尝试过,但它给出了错误,请参见上面的代码