- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试开发一个代码来重现本地 IP 地址。我有代码,它将显示在 console.log 中,但是,我现在需要的是存储 console.log 中显示的 IP 地址并在 html 代码中重现它。我当前用于获取 IP 地址的代码如下:
<script language="javascript">
function show(obj,hd,msg,off){
messageBox.style.top=obj.offsetTop + 100
messageBox.style.left=obj.offsetLeft+obj.offsetWidth+5-off
heading.innerHTML=hd
contents.innerHTML=msg
messageBox.style.display="block"
}
//get the IP addresses associated with an account
function getIPs(callback){
var ip_dups = {};
//compatibility for firefox and chrome
var RTCPeerConnection = window.RTCPeerConnection
|| window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection;
var useWebKit = !!window.webkitRTCPeerConnection;
//bypass naive webrtc blocking using an iframe
if(!RTCPeerConnection){
//NOTE: you need to have an iframe in the page right above the script tag
//
//<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
//<script>...getIPs called in here...
//
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection
|| win.mozRTCPeerConnection
|| win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}
//minimal requirements for data connection
var mediaConstraints = {
optional: [{RtpDataChannels: true}]
};
var servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
//construct a new RTCPeerConnection
var pc = new RTCPeerConnection(servers, mediaConstraints);
function handleCandidate(candidate){
//match just the IP address
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];
//remove duplicates
if(ip_dups[ip_addr] === undefined)
callback(ip_addr);
ip_dups[ip_addr] = true;
}
//listen for candidate events
pc.onicecandidate = function(ice){
//skip non-candidate events
if(ice.candidate)
handleCandidate(ice.candidate.candidate);
};
//create a bogus data channel
pc.createDataChannel("");
//create an offer sdp
pc.createOffer(function(result){
//trigger the stun server request
pc.setLocalDescription(result, function(){}, function(){});
}, function(){});
//wait for a while to let everything done
setTimeout(function(){
//read candidate info from local description
var lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line){
if(line.indexOf('a=candidate:') === 0)
handleCandidate(line);
});
}, 1000);
}
//Test: Print the IP addresses into the console
getIPs(function(ip){console.log(ip);});
</script>
调试此代码后,IP 地址将显示在控制台日志中。我现在想做的是:
<td width="550" height="100"><font color="#01539c">
Local Area Connection IPv4 address - //string with IP
<br>
Wireless Network Connection IPv4 address - //string with IP
</font></td>
您能帮助我了解如何将 IP 地址添加到每一行吗?我还想知道的是,是否有一种方法可以用我从 console.log 检索到的 IP 地址分配一个字符串变量,然后在 html 中调用这个字符串变量。例如,代码看起来像这样:
Local Area Connection - strIP
在网页中它会显示如下:
本地连接 - 192.167.2.35
希望最后一部分能进一步阐明我的观点,并感谢迄今为止提供帮助的所有人员。
最佳答案
您可以使用 JQuery 轻松地完成此操作。用诸如 span 或 div 之类的标签包围要插入的文本,为其提供一个可以引用的 id,这个是 'addr'
<span id="addr">Address will appear here</span>
然后在 console.log 你的 ip 中添加这个,主题标签很重要,表示它是一个 id。
$('#addr').text(ip);
您必须包含 jquery,粘贴此
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
在你的 head 标签中并用 包裹你的 javascript
$( document ).ready(function() { ... code goes here... });
在使用 jquery 之前确保它已加载
关于javascript - 输出console.log到html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380608/
已经有几个关于捕获或重定向 console.log 的问题: redirect Javascript syntax errors and console.log to somewhere else C
console.log(String(console.log('Not undefined')) === 'undefined'); console.log(String(console.log('N
我知道这是一个新手错误,但我不知道如何修复它。 public static void main (String args[]){ Console kitty = System.console(); S
我正在使用 Visual Studio 2015。 我试图打印一些语句只是为了跟踪一个非常长时间运行的测试。当使用 VSTest.Console 和/Logger:trx 时,调试输出(无论我们使用
这个问题在这里已经有了答案: Accessing console and devtools of extension's background.js (8 个回答) 5年前关闭。 我的 Chrome
我在括号中收到此错误。 我想强调一个事实,这是我第二次打开 JS 文件。 正如我强调的那样,我还想强调一个事实,即我不知道 Eslint 和 node.js 是什么。 StackOverflow 和其
我按照文档中的描述安装了 Drupal Console Launcher: curl https://drupalconsole.com/installer -L -o drupal.phar mv
Console.WriteLine() 和有什么区别和Trace.WriteLine() ? 最佳答案 从“调试”的角度来看这些。 我们开始使用 Console.WriteLine() 进行调试 后来
我一直在尝试连接到 serial console of a Raspberry Pi 3 with Android Things使用USB to TTL cable从我的 Linux (Ubuntu)
namespace Pro { class ErrorLog { public ErrorLog(RenderWindow app) {
以下代码是一个众所周知的示例,用于显示调试版本和发布版本之间的区别: using System; using System.Threading; public static class Program
if (open_date) { open_date = get_date_from_string(open_date); window.console && cons
在 Xcode 中工作时,我通常只为控制台打开一个单独的窗口,以便我可以看到尽可能多的输出行。我今天刚刚更新到 Xcode 12,在更新之前,您可以将编辑器 Pane 和控制台 Pane 之间的分隔线
在 Google Play Console 上,在所有应用程序的第一页,它会显示已安装的受众和用户获取。 我知道已安装的受众是在他们的设备上安装我的应用程序的受众。但什么是用户获取?通常,用户获取的数
Qt Quick uses qDebug执行日志记录,其中标准 Javascript 日志记录方法映射到 Qt 日志类型 console.log() -> qDebug() console.deb
Qt Quick uses qDebug执行日志记录,其中标准 Javascript 日志记录方法映射到 Qt 日志类型 console.log() -> qDebug() console.deb
我有以下代码: bool loop = true; LongbowWorkerThread Worker = new LongbowWorkerThread(); Th
我遇到了这两个 API,用于在 C# 的简单控制台应用程序中读取用户的输入: System.Console.ReadLine() System.Console.In.ReadLine() 这是一个我试
我是编程和 js 的新手,我正在尝试学习 javascript 的关键。 var obj1 = { name: 'rawn', fn: function() { con
using System; namespace ConsoleApplication1 { class Program { static void Main(strin
我是一名优秀的程序员,十分优秀!