Javascript 需要将document.getElementsByTagName替换为document.getElementById
我在网上找到了以下代码:Javascript 需要将document.getElementsByTagName替换为document.getElementById,javascript,jquery,Javascript,Jquery,我在网上找到了以下代码: function getIPs(callback){ var ip_dups = {}; var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var useWebKit = !!window.webkitRTCPeerConnection; if(!RTCPeerConnecti
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(“本地ip”).innerHTML=ip
及
document.getElementById(“local-ip2”).value=ip
//这里作为价值
我花了很多时间在它上面,但没有成功…您的函数的if()没有后面的{}大括号,这意味着只执行if()后面的语句,因此代码的可读性较差
我已经将创建,text content=ip,appendChild放在一起,所以应该直接看到发生了什么
只需要通过regExp
getIPs=function(){
var ip=document.getElementById('local-ip2')。值;
//本地IP
如果(ip匹配(/^(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);
}
};代码>
//这里作为价值
获取IP
我想你已经回答了自己的问题。如果不需要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;
}
};
作语法分析
这篇文章只是澄清了一些你可能忽略的事情。你可以使用document.getElementById(“本地ip”).appendChild(localip)代码>,但您需要更改其中一个元素的ID,因为ID必须是唯一的。是的,它可以工作,但我需要删除不需要的span元素…另外,在输入字段中具有相同的值。我在提供的HTML中没有看到任何span元素。你的问题的可回答部分已经有了答案。如果您还有其他问题,请在一个新问题中提供答案,该问题将复制issue.var localip=document.createElement(“span”);是的,但在我的问题中,我还要求删除我不需要的appendChild(span元素)。。。你能更新你的答案吗?我必须在文本字段中插入一个ip!现在看全部代码。。。我只需要编辑期末考试part@Devilix这是一个大编辑。但这一准则仍然适用。你可以把它放在任何你想要的地方。我添加了带有默认值的userIP文本框来捕获用户输入。ip不再是html元素了。检查所有这些代码,打印一个本地ip用户,也许你不了解所有的事情。。。检查一下,谢谢!现在它工作了!但对于输入字段?看这里!如果语句{},可能需要为您添加大括号
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>