Javascript 仅对特定 html 元素启用刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10841239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:10:42  来源:igfitidea点击:

Enabling refreshing for specific html elements only

javascripthtmlajax

提问by Dexter Schneider

I would like to know how I can only refresh a specific element in my website, instead of the whole web page? The element I'm talking about is a flash application that loads quite slow and may experience connection timeouts. I want to enable the user to only refresh that element/falsh app. How do i do that? Below I have an Ajax function that updates an HTML element but not sure how to apply it to my situation.

我想知道如何只刷新网站中的特定元素,而不是整个网页?我正在谈论的元素是加载速度很慢并且可能会遇到连接超时的 Flash 应用程序。我想让用户只刷新那个元素/falsh 应用程序。我怎么做?下面我有一个更新 HTML 元素的 Ajax 函数,但不确定如何将它应用于我的情况。

<head>
    <script type="text/javascript">
        function loadXMLDoc() {
            var xmlhttp;
            if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else { // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "ajax_info.txt", true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <div id="myDiv">
        <h2>Let AJAX change this text</h2>
    </div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>

回答by DextrousDave

Try this:

尝试这个:

function reload(){
    var container = document.getElementById("yourDiv");
    var content = container.innerHTML;
    container.innerHTML= content; 
    
   //this line is to watch the result in console , you can remove it later 
    console.log("Refreshed"); 
}
<a href="javascript: reload()">Click to Reload</a>
    <div id="yourDiv">The content that you want to refresh/reload</div>

Hope it works. Let me know

希望它有效。让我知道

回答by JudeJitsu

Try creating a javascript function which runs this:

尝试创建一个运行这个的 javascript 函数:

document.getElementById("youriframeid").contentWindow.location.reload(true);

Or maybe use an HTML workaround:

或者也许使用 HTML 解决方法:

<html>
<body>
<center>
      <a href="pagename.htm" target="middle">Refresh iframe</a>
      <p>
        <iframe src="pagename.htm" name="middle">
      </p>
</center>
</body>
</html>

Both might be what you're looking for...

两者都可能是您正在寻找的...

回答by Pratiksha Vaishnav

Try this in your script:

在你的脚本中试试这个:

$("#YourElement").html(htmlData);

I do this in my table refreshment.

我在我的餐桌茶点中这样做。

回答by omarbelguith

try to empty your innerHtml everytime. just like this:

每次都尝试清空你的innerHtml。像这样:

Element.innerHtml="";