Chrome 在链接到不存在的文件后未重新加载对象代码

我有以下索引.html。下面的javascript的目标是重新加载元素的数据标签,以便它可以显示多个图像。但是,我链接按钮的图像之一可能不存在(在本例中为 )。#obj#2


function updateObject(evt) {

    var id = evt.currentTarget.id;

    var object = document.getElementById("obj");


    if (id == "1") {

          object.setAttribute("data","https://upload.wikimedia.org/wikipedia/commons/f/fa/Apple_logo_black.svg")

    }

    else {

        object.setAttribute("data", "file/that/doesnt/exist")

    }

}


for (var i = 0; i <   document.getElementsByTagName("button").length; i++) {

    document.getElementsByTagName("button")[i].addEventListener("click", updateObject, false);

}

<!DOCTYPE html>

<html>

    <head>

        <title>Home</title>

        <meta name='viewport' content='width=device-width, initial-scale=1.0'>

    </head>

    <body>

        <button id="1">button1</button>

        <button id="2">button2</button>

        <object id="obj" style='width: 100px'></object>

    </body>

</html>

我期望在以下脚本中发生的事情是这样的:

  • 用户按下按钮1,看到苹果

  • 用户按下按钮2,什么也没看到

  • 用户按下按钮1,看到苹果

但是,第三步没有发生 - 当我在链接到不存在的文件后尝试重新加载对象的数据时,它保持空白。

据我所知,这发生在Chrome中,对我来说,这是在Safari中工作的。我必须使用对象标签或其他一些允许交互式SVG的方法。


GCT1015
浏览 77回答 3
3回答

ITMISS

您可以采取的一种解决方案是删除并添加节点本身以强制进行硬重置var clone = object.cloneNode();var parent = object.parentNode;parent.removeChild(object);parent.appendChild(clone);function updateObject(evt) {&nbsp; var id = evt.currentTarget.id;&nbsp; var object = document.getElementById("obj");&nbsp; if (id == "1") {&nbsp; &nbsp; object.setAttribute("data", "https://upload.wikimedia.org/wikipedia/commons/f/fa/Apple_logo_black.svg")&nbsp; &nbsp; var clone = object.cloneNode();&nbsp; &nbsp; var parent = object.parentNode;&nbsp; &nbsp; parent.removeChild(object);&nbsp; &nbsp; parent.appendChild(clone);&nbsp; } else {&nbsp; &nbsp; object.setAttribute("data", "file/that/doesnt/exist")&nbsp; }}for (var i = 0; i < document.getElementsByTagName("button").length; i++) {&nbsp; document.getElementsByTagName("button")[i].addEventListener("click", updateObject, false);}<button id="1">button1</button><button id="2">button2</button><object id="obj" style='width: 100px'></object>

眼眸繁星

尝试将标记更改为 a 并设置“src”属性。<img>function updateObject(evt) {&nbsp; &nbsp; var id = evt.currentTarget.id;&nbsp; &nbsp; var object = document.getElementById("obj");&nbsp; &nbsp; if (id == "1") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.setAttribute("src","https://upload.wikimedia.org/wikipedia/commons/f/fa/Apple_logo_black.svg")&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; object.setAttribute("src", "file/that/doesnt/exist")&nbsp; &nbsp; }}for (var i = 0; i <&nbsp; &nbsp;document.getElementsByTagName("button").length; i++) {&nbsp; &nbsp; document.getElementsByTagName("button")[i].addEventListener("click", updateObject, false);}<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>Home</title>&nbsp; &nbsp; &nbsp; &nbsp; <meta name='viewport' content='width=device-width, initial-scale=1.0'>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <button id="1">button1</button>&nbsp; &nbsp; &nbsp; &nbsp; <button id="2">button2</button>&nbsp; &nbsp; &nbsp; &nbsp; <img id="obj" style='width: 100px'></img>&nbsp; &nbsp; </body></html>

慕后森

我提供了一个示例,通过向该URL发出虚假请求来帮助您解决问题。铬这样做是为了通知。即使您正确处理正确的错误处理,并且每个技巧都带有或被告知要防止错误 - 您也无法修复它。它不受脚本控制。onerrortry-catchvoid( )function updateObject(evt) {&nbsp; &nbsp; var id = evt.currentTarget.id;&nbsp; &nbsp; var object = document.getElementById("obj");&nbsp; &nbsp; if (id == "1") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.setAttribute("data","https://upload.wikimedia.org/wikipedia/commons/f/fa/Apple_logo_black.svg");&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; var request;&nbsp; &nbsp; &nbsp; if(window.XMLHttpRequest)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request = new XMLHttpRequest();&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; request = new ActiveXObject("Microsoft.XMLHTTP");&nbsp; &nbsp; &nbsp; request.open('GET', 'file/that/doesnt/exist', false);&nbsp; &nbsp; &nbsp; request.send();&nbsp; &nbsp; &nbsp; // the object request will be actually modified&nbsp; &nbsp; &nbsp; if (request.status === 404) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("The file you are trying to reach is not available.");&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.setAttribute("data", "file/that/doesnt/exist");&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }}for (var i = 0; i <&nbsp; &nbsp;document.getElementsByTagName("button").length; i++) {&nbsp; &nbsp; document.getElementsByTagName("button")[i].addEventListener("click", updateObject, false);}<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>Home</title>&nbsp; &nbsp; &nbsp; &nbsp; <meta name='viewport' content='width=device-width, initial-scale=1.0'>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <button id="1">button1</button>&nbsp; &nbsp; &nbsp; &nbsp; <button id="2">button2</button>&nbsp; &nbsp; &nbsp; &nbsp; <object id="obj" style='width: 100px'></object>&nbsp; &nbsp; </body></html>但请注意,它仅适用于同一来源。对于另一台主机,您将必须使用服务器端语言来执行此操作,您必须自己弄清楚。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript