我正在尝试从 localStorage
检索一个值并将其附加到 iframe URL 中,然后将其写到页面上。
我正在尝试使用 document.write
渲染 iframe,但我认为我做得不正确。
这是我到目前为止所拥有的:
<script>
jQuery(document).ready(function($) {
$( ".postid-192" ).load(function() {
var email = localStorage.getItem("email");
document.write('<iframe src="http://www2.site.com/l/123/2009-04-28/ABCDE?email=' + email + ' width="1" height="1"></iframe>');
});
});
</script>
我想我对 document.write
应该做什么有点困惑?我有种印象,它会写在页面上……某个地方。但我可能会错误地处理这个问题。总的来说,我试图获取值(在本例中为电子邮件)并将其附加到 iframe URL 中,然后将其放置在页面上的某个位置。我希望这是有道理的。
最佳答案
The write() method is mostly used for testing: If it is used after an HTML document is fully loaded, it will delete all existing HTML.
所以不要使用document.write
。这段代码是正确的:
<script>
$(document).ready(function() {
$(".postid-192").load(function() {
var email = localStorage.getItem("email");
$("#iframe").html('<iframe src="http://www2.site.com/l/123/2009-04-28/ABCDE?email=' + email + '" width="1" height="1"></iframe>');
});
});
</script>
和 HTML:
<div id="iframe"></div>
关于javascript - 从 localStorage 获取值并附加到 iframe URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47103135/