Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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设置innerText只会暂时适用_Javascript_Html - Fatal编程技术网

使用Javascript设置innerText只会暂时适用

使用Javascript设置innerText只会暂时适用,javascript,html,Javascript,Html,所以我在学习Javascript,我遇到了一个问题: 我可以设置段落元素的内部文本。但是马上网络浏览器撤销是我的工作!!这意味着网页完全恢复到它所处的状态,就好像我重新加载了网页一样 <html> <body> <form> <input type="submit" onclick="GetCurrentLocation()" value="Get Current URL" /> &l

所以我在学习Javascript,我遇到了一个问题:

我可以设置段落元素的内部文本。但是马上网络浏览器撤销是我的工作!!这意味着网页完全恢复到它所处的状态,就好像我重新加载了网页一样

<html>
    <body>
        <form>
            <input type="submit" onclick="GetCurrentLocation()" value="Get Current URL" />
        </form>
        <p id="CurrentURL">Current URL:</p>

        <script>
            function GetCurrentLocation()
            {
                var myCurrentLocation = window.location.href;
                var curLocP = document.getElementById("CurrentURL");
                curLocP.innerText = "Current URL: " + myCurrentLocation;
            }
        </script>
    </body>
</html> 

当前URL:

函数GetCurrentLocation() { var myCurrentLocation=window.location.href; var curLocP=document.getElementById(“CurrentURL”); curLocP.innerText=“当前URL:+myCurrentLocation; }
因此,在上面,我在函数的最后一行设置了innertext,在UI中,我可以看到正确的预期文本:

当前URL:C:\Users…\Projects\test.html

闪烁然后消失,只留下我:

当前URL:


我正在Google Chrome上运行此功能。

浏览器提交表单并刷新页面。这就是为什么你放松了改变。将输入类型从
submit
更改为
按钮

<input type="button" onclick="GetCurrentLocation()" value="Get Current URL" />

浏览器提交表单并刷新页面。这就是为什么你放松了改变。将输入类型从
submit
更改为
按钮

<input type="button" onclick="GetCurrentLocation()" value="Get Current URL" />

在JavaScript中添加一个
返回:false
,以防止表单被提交和重新加载页面(并使其看起来像是重新加载时更改消失):



在JavaScript中添加一个
返回:false
,以防止表单被提交和重新加载页面(并使其看起来像是重新加载时更改消失):



浏览器提交表单,这就是刷新发生的原因。 如果需要使用提交按钮,可以使用
event.preventDefault()


浏览器提交表单,这就是刷新发生的原因。 如果需要使用提交按钮,可以使用
event.preventDefault()


非常感谢。它工作得很好!这可能就是我要用的。我很高兴能帮上忙。谢谢。它工作得很好!这可能就是我要用的。我很高兴能帮助大家,非常感谢你们的回答。太快了!不客气。请接受你想要的答案。伙计们,非常感谢你们的回答。太快了!不客气。请接受你想要的答案。非常好!非常感谢,非常好!非常感谢。
function GetCurrentLocation(event)
{
    event.preventDefault();
    var myCurrentLocation = window.location.href;
    var curLocP = document.getElementById("CurrentURL");
    curLocP.innerText = "Current URL: " + myCurrentLocation;
}