onclick加载/运行此代码

时间:2015-07-14 14:08:46

标签: javascript jquery

我正在尝试创建一个html文件,它将在单击按钮上加载/运行以下代码。我曾尝试过如下方法。

            <!-- Custom -->
            <div id="custom1">

            </div>
            <script>
                function custom()
                {
                var dummy = '<div id="custom1" style="display: block; position: absolute; top: 0px; left: 0px; z-index: 9999900; width: 100%; opacity: 0.8; height: 768px; visibility: visible;" ></div>
                <div id="custom1" style="display: block; text-align: center; line-height: normal; visibility: visible; position: absolute; top: 0px; left: 0px; z-index: 9999910; width: 100%; height: 667px;" ><div id="close_button_div" align="center" style="position: absolute; width: 100%; z-index: 9999930;"></div><div style="background: transparent; height: 768px; z-index: 9999915;">
                    <iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO" ></iframe>
                </div>
                </div>
                <div id="custom1" align="center" style="position: absolute; width: 100%; z-index: 9999930;" ></div>
                <div id="custom1" style="background: transparent; height: 768px; z-index: 9999915;" >
                    <iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="captcha.php" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO">
                    </iframe>
                </div>
            </div>';
                document.getElementById('custom1').innerHTML += dummmy;
                }
            </script>
            <!-- END custom-->

这是我用于按钮onclick

的代码
<a href="#" onclick="custom();">DOWNLOAD</a>

它不适合我。

3 个答案:

答案 0 :(得分:1)

在innerHTML之后删除 +

function custom()
{
      var dummy = '<div id="custom1" style="display: block; position: absolute; top: 0px; left: 0px; z-index: 9999900; width: 100%; opacity: 0.8; height: 768px; visibility: visible;" ></div><div id="custom1" style="display: block; text-align: center; line-height: normal; visibility: visible; position: absolute; top: 0px; left: 0px; z-index: 9999910; width: 100%; height: 667px;" ><div id="close_button_div" align="center" style="position: absolute; width: 100%; z-index: 9999930;"></div><div style="background: transparent; height: 768px; z-index: 9999915;"><iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO" ></iframe></div>
</div><div id="custom1" align="center" style="position: absolute; width: 100%; z-index: 9999930;" ></div><div id="custom1" style="background: transparent; height: 768px; z-index: 9999915;"><iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="captcha.php" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO">                 </iframe></div></div>';                    document.getElementById('custom1').innerHTML = dummmy;
}

演示:

http://jsfiddle.net/n70o70tz/

答案 1 :(得分:0)

可能是语法错误,请尝试使用 + 运算符连接各行。

<script>
function custom()
 {
  var dummy = '<div id="custom1" style="display: block; position: absolute; top: 0px; left: 0px; z-index: 9999900; width: 100%; opacity: 0.8; height: 768px; visibility: visible;" ></div>'+
                '<div id="custom1" style="display: block; text-align: center; line-height: normal; visibility: visible; position: absolute; top: 0px; left: 0px; z-index: 9999910; width: 100%; height: 667px;" ><div'+ 'id="close_button_div" align="center" style="position: absolute; width: 100%; z-index: 9999930;"></div><div style="background: transparent; height: 768px; z-index: 9999915;">'+
                    '<iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO" ></iframe>'+
     '</div></div><div id="custom1" align="center" style="position: absolute; width: 100%; z-index: 9999930;" ></div>'+
                '<div id="custom1" style="background: transparent; height: 768px; z-index: 9999915;" >'+
                    '<iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="captcha.php" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO">'+
       '</iframe> </div> </div>';
                document.getElementById('custom1').innerHTML += dummmy;
                }
 </script>
<!-- END custom-->

答案 2 :(得分:0)

您必须在假人之前删除 + 符号。您的代码中存在 dummmy 的变量名称拼写错误。 这是工作代码

&#13;
&#13;
 function custom()
                {
                var dummy= '<div id="custom1" style="display: block; position: absolute; top: 0px; left: 0px; z-index: 9999900; width: 100%; opacity: 0.8; height: 768px; visibility: visible;" ></div> <div id="custom1" style="display: block; text-align: center; line-height: normal; visibility: visible; position: absolute; top: 0px; left: 0px; z-index: 9999910; width: 100%; height: 667px;" ><div id="close_button_div" align="center" style="position: absolute; width: 100%; z-index: 9999930;"></div><div style="background: transparent; height: 768px; z-index: 9999915;"> <iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO" ></iframe>  </div> </div>                <div id="custom1" align="center" style="position: absolute; width: 100%; z-index: 9999930;" ></div>  <div id="custom1" style="background: transparent; height: 768px; z-index: 9999915;" ><iframe id="custom1" width="100%" height="768" id="overlay_iframe" src="captcha.php" allowtransparency="true" frameborder="0" style="position: relative; height: 768px; overflow: hidden; z-index: 9999920; display: block; background-color: transparent;" scrollbars="NO">  </iframe> </div>  </div>';
                document.getElementById('custom1').innerHTML = dummy;
                }
&#13;
 <div id="custom1">
 </div>
 <a href="#" onclick="custom();">DOWNLOAD</a>
&#13;
&#13;
&#13;