【问题标题】:innerHTML onClick issueinnerHTML onClick 问题
【发布时间】:2013-01-19 08:55:51
【问题描述】:

我正在尝试使用子页面更改作为父页面一部分的 div 的内容。为了更新内容,我使用了 innerHTML。内容看起来很好,但图像上的 onclick 事件不起作用。这是我的代码,该代码在 Mozilla firefox 中有效,但在 Chrome 和 Safari 中无效。此外,它不适用于 iPad Safari 浏览器。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>TEMP</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
        <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
        <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
        <script src="scripts/jquery-1.8.2.min.js"></script>         
        <script>
            $(document).ready(function() {
                    //this is the parent element
                    parent.document.getElementById('hd').innerHTML = document.getElementById('temp').innerHTML;
            });
        </script>
    </head>
    <body>
        <div id="fakeshare" style="float: left;margin-top: -30px;z-index:1000000;position: absolute;vertical-align: middle">    
        </div>
        <!-- this is the content I try to populate in the parent element -->
        <div id = "temp" style="visibility: hidden">
        <div style="float:left">
            <img id="backbtn2" class="imgtopbutton" src="images/facebook-cancel.png" onclick="javascript:history.go(-1)" align="left" style="margin-left:12px;margin-top:10px"/>
            <img class="imgtopbutton" src="images/facebook-post.png" align="right" style="margin-top:-31px;margin-left:545px" onclick="javascript:history.go(-1)">
        </div>
        </div>
    </body>
</html>

【问题讨论】:

  • 带有id="hd"的元素在哪里?
  • 父页面和子页面是什么意思?你在用iframe吗?
  • 它是父页面的一部分。
  • 我正在使用模态对话框。子页面在模态对话框中加载,hd 元素是父页面的一部分,作为此模态对话框的标题。
  • 父页面和子页面是否在同一个域中?

标签: html cross-browser onclick innerhtml


【解决方案1】:

试试吧:

$("#hd").html($("#temp").html());

【讨论】:

  • 好的,需要查看你的父 HTML。
  • 试试console.log(document);,看看你在文档的DOM中看到了什么。
  • 我解决了我的问题。但是,对于我的答案,问题会有所不同,所以我会标记你对这个问题的正确性。
  • @VendettaDroid 你能解释一下实际的问题是什么吗?
【解决方案2】:

尝试使用 $('#hd').parent().html($('#temp').html());

【讨论】:

  • 我试过了,但它并没有更新#hd的内容。从 chromes 调试视图查看内容。
猜你喜欢
  • 2023-03-11
  • 2014-04-10
  • 2015-03-10
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 2011-02-06
相关资源
最近更新 更多