【问题标题】:javascript select iframe by idjavascript 通过 id 选择 iframe
【发布时间】:2015-12-20 08:17:09
【问题描述】:

这不是不可能的,但我已经花了很多时间在这里寻找答案并尝试组合。

我有多个由代码生成的 iframe,每个 iframe 都有一个唯一的 ID。我希望能够通过按钮或双击来阻止某些 iframe 加载。

<iframe id='kwjef' src='kwjef.html'
   onClick="stop_iframe($(this).attr('id'));" />
<iframe id='wefhj' src='wefhj.html' 
   onClick="stop_iframe($(this).attr('id'));" />
<iframe id='yhvem' src='yhvem.html' 
   onClick="stop_iframe($(this).attr('id'));" />

function stop_iframe(id) {
  window.frames[0].stop(); /* works to stop first iframe */
  window.frames[1].stop(); /* works to stop second iframe */
  window.getElementById(id).stop(); /* throws error */
  $('iframe#'+id).stop(); /* throws error */
}

如何通过 id 正确选择 iframe 以便调用 stop()?

编辑(thnxz Vicky Gonsalves):

function stop_iframe(id) {
  document.getElementById(id).src="#";
}

这可以使 iframe 空白,但不能真正停止请求,window.frames[0].stop();确实会停止请求(例如浏览器中的停止按钮)。

【问题讨论】:

    标签: javascript iframe jquery-selectors selector


    【解决方案1】:

    不要试图停止,只需将 iframe 的 src 替换为 #empty URL:

    function stop_iframe(id) {
      document.getElementById(id).src="#";
    }
    

    【讨论】:

    • 我忘记了在我最初的帖子中,这可以使 iframe 空白但不会真正停止请求,window.frames[0].stop(); 确实会停止请求(就像浏览器中的停止按钮一样)。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多