试图瞄准一个按钮

时间:2017-11-11 15:35:14

标签: javascript html iframe

嗨我正在尝试使用iframe将按钮定位到div ..我尝试了很多但没有结果......

 <html>
 <body>
  <div id="one">
 <button type="submit" onclick="ShowResult()" formtarget="result"> Calculate </button> 
  </div>
  <div id="two">
  <iframe name="result"></iframe>
  </div>
  </body>

的javascript:

  function ShowResult()
  {
    document.write("someword");
   }

似乎是真的,但当我点击按钮时,它会打开一个空白页面,显示函数中的内容..帮助??

1 个答案:

答案 0 :(得分:0)

你遗失了很多,

  • 首先不再练习"onclick()"
  • 第二次不再练习"document.write"

我已经重写了你的代码,享受......

<强> HTML

<div id="one">
    <button type="submit" id="functionThis" formtarget="result"> Calculate </button>
</div>
<div id="two">
    <iframe id="iframe1" name="result"></iframe>
</div>

<强> JS

 document.getElementById("functionThis").addEventListener("click", showResult, false);

 function showResult() {
     var htmlString = "<body>Some Words</body>";
     var myIFrame = document.getElementById('iframe1');
     myIFrame.src = "javascript:'" + htmlString + "'";
 }

<强>小提琴

working fiddle