需要将document.getElementsByTagName替换为document.getElementById

时间:2016-12-19 22:02:35

标签: javascript jquery

我在网上找到了这段代码:

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

我花了很多时间,没有成功......

2 个答案:

答案 0 :(得分:1)

你的函数有if()没有跟随{}大括号,这意味着只执行if()后面的语句,所以使代码的可读性降低。

我把创作,text content = 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>

答案 1 :(得分: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>

这篇文章只是澄清你可能忽略的事情。

相关问题