Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在innerHTML中放置超链接?_Javascript - Fatal编程技术网

Javascript 如何在innerHTML中放置超链接?

Javascript 如何在innerHTML中放置超链接?,javascript,Javascript,看起来是这样的: Q:如何使超链接正常工作 它只显示html代码更改: <!DOCTYPE html> <html style="height: 99%;"> <meta charset=utf-8> <body style="height: 99%;"> <table style="height: 99%;" align=center> <tr> <td>

看起来是这样的:

Q:如何使超链接正常工作

它只显示html代码更改:

<!DOCTYPE html>
<html style="height: 99%;">
<meta charset=utf-8>
<body style="height: 99%;">
    <table style="height: 99%;" align=center>
        <tr>
            <td>
                <p id="sample" style="font-size:300%"></p>
                <script>
                    var myArray = [
                        'first',
                        'second',
                        'third',
                        'fourth'
                    ];
                    var rand = Math.floor(Math.random() * myArray.length)
                    var concat = myArray[rand]
                    concat = '&lt;a href=https://www.google.com/#q='+concat+'&gt;'+concat+'&lt;/a&gt;'
                    function random() {
                        document.getElementById("sample").innerHTML = (concat);
                    }
                    random()
                </script>
            </td>
        </tr>
    </table>
</body>
</html>
致:

concat=''

如果您想将其解析为HTML,不要逃避它。换句话说,您特意使用了
这是HTML…就像您在页面的其余部分中放置的一样…所以要像HTML而不是文本一样编写它
concat = '&lt;a href=https://www.google.com/#q='+concat+'&gt;'+concat+'&lt;/a&gt;'
concat = '<a href=https://www.google.com/#q=' + concat + '>' + concat + '</a>'