我已經在網上找到這段代碼:需要更換document.getElementsByTagName到的document.getElementById
function getIPs(callback){
var ip_dups = {};
var RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection;
var useWebKit = !!window.webkitRTCPeerConnection;
if(!RTCPeerConnection){
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
};
var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
var pc = new RTCPeerConnection(servers, mediaConstraints);
function handleCandidate(candidate){
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/
var ip_addr = ip_regex.exec(candidate)[1];
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);
ip_dups[ip_addr] = true;
}
pc.onicecandidate = function(ice){
//skip non-candidate events
if(ice.candidate)
handleCandidate(ice.candidate.candidate);
};
pc.createDataChannel("");
pc.createOffer(function(result){
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});
setTimeout(function(){
var lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
}, 1000);
}
//insert IP addresses into the page
getIPs(function(ip){
var localip = document.createElement("span");
localip.textContent = ip;
//local IPs
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/))
document.getElementsByTagName("h3")[1].appendChild(localip);
});
現在我需要改變document.getElementsByTagName
到document.getElementsById
,除去document.createElement("span")
,並且只有結果,因爲我需要在兩個地方打印它。
例如:
document.getElementById("local-ip").innerHTML = ip
<div id="local-ip"></div>
和
document.getElementById("local-ip2").value = ip
<input type="text" id="local-ip2" /> //here as value
我花了很多時間就可以了,沒有成功...
您可以使用'document.getElementById(「local-ip」)。appendChild(localip);',但您需要更改其中一個元素的ID,因爲ID **必須是唯一的。 – blex
是的,它的工作原理,但我需要刪除我不需要的span元素...加上,在輸入字段中具有相同的值 – Devilix
我沒有看到HTML中提供的任何span元素。你有問題的答案部分的答案。如果您還有其他問題,請在新問題中提供[mcve],以複製問題。 –