javascript - 无法在 html 页面中显示从 javascript 函数返回的 nslookUp 值?

标签 javascript html css

<分区>

我正在尝试显示来自 javascript 函数的 nslookup 值,该函数在加载页面时将 nslookup 值返回给 html。

我在 html 页面中有以下脚本:

<html>
<style type="text/css">
    .Table
    {
        display: table;
    }
    .Title
    {
        display: table-caption;
        text-align: center;
        font-weight: bold;
        font-size: larger;
    }
    .Heading
    {
        display: table-row;
        font-weight: bold;
        text-align: center;
    }
    .Row
    {
        display: table-row;
    }
    .Cell
    {
        display: table-cell;
        border: solid;
        border-width: thin;
        padding-left: 20px;
        padding-right: 20px;
    }
</style>
<body onload="myFunction()">
<h1>"The dns ip is: " <span id="myID"></span></h1>

    <div class="Heading">
        <div Class="Cell">
            <p>G</p>
        </div>
        <div Class="Cell">
            <p>facebook.com</p>
        </div>
        <div id="test">
            <span id="h" class="Cell">
                <p>H</p>
            </span>
            <span id="e" class="cell">
                <p>E</p>
            </span>
           <span id ="Time" class="Cell">   
                <P> </P>                
             </span>
        </div>


        <div Class="Cell">
            <p></p>
        </div>
    </div>

</body>



<button type="button" onclick="myFunction()">Refresh</button> 

<script>
function myFunction()
 {

    //document.body.style.backgroundColor = "red";
    document.getElementById("h").style.background="green";
    const dns = require('dns');
    var status="";
    dns.lookup('facebook.com', function(err, result){
     var status = result;
    });
     document.getElementById("Time").innerHTML=status;
}
</script> 

</html>



预期: 我希望获得要返回的 url facebook.com 的 dns 值并显示在“The dns ip is:”字段中

实际: 没有任何内容从脚本返回到那里显示

感谢是否有人可以提供帮助?

最佳答案

function myFunction()
 {
    const dns = require('dns');
    dns.lookup('https://facebook.com', function(err, result){
     var status = result
     document.getElementById("myID").innerHTML=status;
    });
}

关于javascript - 无法在 html 页面中显示从 javascript 函数返回的 nslookUp 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57667864/

上一篇:html - 在 HTML/CSS 中访问链接时使图像变成灰度

下一篇:html - 如何删除html div元素之间的间距?

相关文章:

c# - 在没有 AJAX 的情况下使用 Javascript 的服务器端方法

javascript - 如何有条件地抑制 Express(或 Connect)中的日志记录?

html - 通过 CSS 替换 valign

javascript - jquery 乘法 - 幕后计算

html - .float right 类不会向右浮动

javascript - 如何将ext js 4.0面板导出成图片格式?

javascript - 为什么字符串中的等号会抛出意外的标记错误?

html - 垂直堆叠元素的基线

html - ul 中的文本居中,带有 float 的 li's

html - 将子div放在父div的底部