【问题标题】:Get value from another #document JavaScript !Doctype从另一个 #document JavaScript !Doctype 获取价值
【发布时间】:2022-02-05 05:47:51
【问题描述】:

如何从另一个 iframe 中获取价值?

当我检查网站并点击此行时:

<input type="hidden" name="1" value="q"›

然后我在控制台输入这个:

document.getElementsByName("3")[0].value

我可以得到值:

'e'

但是,如果我从 iframe 外部单击,并检查值,我会得到以下结果: VM89205:1 未捕获的类型错误:无法读取未定义的属性(读取“值”) 在:1:49

如果我想获得价值,我必须点击里面。是不是每次都点进去就可以拿到值?

<iframe name="frame" id="frame" src="TEST.html?token=asdasdasd" style="display: none;"> ==$0 
    #document
      <html>...</html>
     </iframe>                                                                                    
    <form name="apple" id="apple" method="get" action="test.htm?token=asdasdasd" target="dialog">  <input type="hidden" name="1" value="q"›
      <input type="hidden" name="1" value="q"›
      <input type="hidden" name="2" value="wm> 
      <input type="hidden" name="3" value="e"> 
      <input type="hidden" name="4" value="r"› 
      <input type="hidden" name="5" value="e> 
      <input type="hidden" name="6" value="z"> 
      <input type="hidden" name="7" value>
      <input type="hidden" name="8" value="u"> 
      <input type="hidden" name="9" value="i"> 
      <input type="hidden" name="10" value>

【问题讨论】:

    标签: javascript html dom


    【解决方案1】:

    要从 iframe 外部访问 iframe 的内容(前提是它不是外部域),请使用 document.getElementById("frame").contentWindow.document.getElementsByName("3")[0].value;

    【讨论】:

      【解决方案2】:

      添加名称标签

      <input type="hidden" name="1" name="q" value="q"›
      



      然后键入此值以获取值
      var v = document.getElementById("q").innerHTML;
      console.log(v);
      

      【讨论】:

      • 不幸的是,这种方式现在有效。代码不允许我给出 2 个名字。当我尝试获取该值时,会弹出此错误消息: Uncaught TypeError: Cannot read properties of null (reading 'innerHTML') at :1:51
      • q 仍在输入元素中,不会有innerHTML。就像@zsolti900 所说,不允许重复的名称属性。
      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多