更改img并保存cookie html& JavaScript的

时间:2018-05-17 06:20:54

标签: javascript html html5 cookies

此代码替换图像并保留cookie

有错吗?我不知道解决方案。



<html>

<button onclick="hideImage()"> Remove img </button>
<img id="my_images" src="http://karachiairport.com.pk/images/data-section/airline/24-11.png">
<script>

document.addEventListener('DOMContentLoaded', function() {
    // If cookie exist
    if (document.cookie.indexOf('image_clicked') > -1) {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
    }
	

    function hideImage() {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
        document.cookie = "image_clicked=true";
    }
});


</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

请试一试。

现在&#39; hideImage未定义&#39;错误已解决。

&#13;
&#13;
<html>

<button onclick="hideImage()"> Remove img </button>
<img id="my_images" src="http://karachiairport.com.pk/images/data-section/airline/24-11.png">
<script>

document.addEventListener('DOMContentLoaded', function() {
    // If cookie exist
    if (document.cookie.indexOf('image_clicked') > -1) {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
    }
	

   
});

 function hideImage() {
        document.getElementById("my_images").src="http://www.1dmag.com/all_asset/img/no-image.png";
        document.cookie = "image_clicked=true";
    }
</script>
&#13;
&#13;
&#13;