2016-12-19 23 views
1

我已經在網上找到這段代碼:需要更換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.getElementsByTagNamedocument.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 

我花了很多時間就可以了,沒有成功...

+1

您可以使用'document.getElementById(「local-ip」)。appendChild(localip);',但您需要更改其中一個元素的ID,因爲ID **必須是唯一的。 – blex

+0

是的,它的工作原理,但我需要刪除我不需要的span元素...加上,在輸入字段中具有相同的值 – Devilix

+0

我沒有看到HTML中提供的任何span元素。你有問題的答案部分的答案。如果您還有其他問題,請在新問題中提供[mcve],以複製問題。 –

回答

1

你的功能,如果有()沒有按照{ }大括號,這意味着只有if()後面的語句被執行,所以使得代碼不易讀。

我已經將創建,文本內容= ip,appendChild全部放在一起,所以應該直接看看發生了什麼。

只需要通過regExp。

getIPs = function() { 
 

 
var ip = document.getElementById('local-ip2').value; 
 
    
 
//local IPs 
 

 
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) { 
 
     //document.getElementsByTagName("h3")[1].appendChild(localip); 
 
     var localip = document.createElement("span"); 
 
      localip.textContent = ip; 
 
      document.getElementById('local-ip').appendChild(localip); 
 
    } 
 
};
<div id="local-ip"></div> 
 
<input type="text" id="local-ip2" /> //here as value 
 
<button onclick="getIPs()">Get IP</button>

+0

是的,但在我的問題中,我也要求刪除appendChild(span元素),我不需要......你能更新你的答案嗎? – Devilix

0

我想你已經回答了你自己的問題。如果不需要span,則可以高興地避免創建它。

function parse() { 
    var ip = document.getElementById('userIP').value; 
    if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) { 
     document.getElementById("local-ip2").value = ip; 
     document.getElementById("local-ip").innerHTML = ip; 
    } 
}; 

<input type="text" id="userIP" value="192.168.1.1" /> 
<button onclick="parse()">Parse</button> 

這個帖子只是澄清,你可能忽略的東西。

+0

我必須插入一個IP,進入文本字段!看看現在已經退役的代碼...我只需要編輯最後的部分 – Devilix

+0

@Devilix這是一個很大的編輯。但是這個代碼仍然成立。你可以把它放在你想要的任何地方。我添加了默認值的userIP文本框來捕獲用戶輸入。 ip不再是一個html元素。檢查了這一點https://jsfiddle.net/2194ft05/ – Searching

+0

所有的代碼,打印本地IP用戶,也許你不明白一切...檢查此https://jsfiddle.net/2194ft05/1/ – Devilix

相關問題