Html iframe 的替代文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8252676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:39:01  来源:igfitidea点击:

Alternate text for iframes

htmljspiframealt

提问by Anuj Balan

We have alternate text, altattribute, for an imgtag in HTML, which will show up when the image doesn't come up. I tried using the tag with iframetoo.

我们有HTML 标签的替代文本alt属性,img当图像没有出现时,它会显示出来。我也尝试使用标签iframe

<iframe src="www.abc.com" alt="Web site is not avaialable">

But the alternate text doesn't come up when src=""is given. Just wanted to know if I can achieve alternate text in any other way, if the srcis not given ?

但是当src=""给出替代文本时不会出现。只是想知道我是否可以以任何其他方式实现替代文本,如果src没有给出?

采纳答案by Jake Feasel

Since my first attempt misunderstood your question, let's try this instead:

由于我的第一次尝试误解了您的问题,让我们试试这个:

<script>
$(function () {

    $("iframe").not(":has([src])").each(function () {

    var ifrm = this;

    ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;

    ifrm.document.open();
    ifrm.document.write($(this).attr("alt"));
    ifrm.document.close();

    });

});
</script>

This will read the "alt" tag value for any iframe with either no src attribute or a src attribute with a blank value, and write the alt text into the body of that iframe.

这将读取没有 src 属性或 src 属性为空值的任何 iframe 的“alt”标记值,并将 alt 文本写入该 iframe 的正文中。

Assist from Write elements into a child iframe using Javascript or jQuery

使用 Javascript 或 jQuery协助将元素写入子 iframe

回答by nosajimiki

While not the "cleanest" solution, another option is to use position and z-index in your CSS to "hide" an empty image under the iframe. This will give you all of the meta-data advantages of true alt text without needing to go into any complex scripting.

虽然不是“最干净”的解决方案,但另一种选择是在 CSS 中使用位置和 z-index 来“隐藏” iframe. 这将为您提供真正的替代文本的所有元数据优势,而无需编写任何复杂的脚本。

It's also good if you just want it as placeholder to display until the iframeis done loading.

如果您只是希望它作为占位符显示直到iframe加载完成,这也很好。

回答by Brandon Gano

The <iframe> element doesn't support an alt attribute, but it does support longdesc. Still, the HTML specification does not dictate how browsers handle long description (or alternate) text. The only way to guarantee any specific behavior is to use JavaScript. Here is an untested example using jQuery:

<iframe> 元素不支持 alt 属性,但它支持 longdesc。尽管如此,HTML 规范并未规定浏览器如何处理长描述(或替代)文本。保证任何特定行为的唯一方法是使用 JavaScript。这是一个使用 jQuery 的未经测试的示例:

// Not tested
$('iframe').each(function() {
  if ($(this).attr('href') == '') {
    // Do something with $(this).attr('longdesc')
  }
});

回答by Jake Feasel

I don't know of a way to trap for 404 responses from an iframe in any kind of straightforward way, however you could trap it with some jQuery:

我不知道有什么方法可以以任何直接的方式从 iframe 捕获 404 响应,但是您可以使用一些 jQuery 捕获它:

<iframe id="myFrame"></iframe>

<script>
$(function () {

  $.ajax({
     url: "http://www.abc.com",
     success: function (data) {
        $("#myFrame").html(data);
     },
     error: function () {
        $("#myFrame").html("Web site is not avaialable");
     }

});
</script>