简体   繁体   English

自动嵌入图片网址的JavaScript可以运行,但出现W3错误

[英]JavaScript for auto embedding image URL works but getting W3 errors

I now where my errors are, I just don't know enough about javascript know where it needs to go, can anyone help me. 现在我的错误在哪里,我只是对javascript不够了解,知道它需要去哪里,任何人都可以帮助我。 I finally got this all working to auto embed media and images on my site (phpbb forum), and it works good, but the image embed isn't correct (even though it does work). 我终于使所有这些工作都可以自动将媒体和图像嵌入到我的网站(phpbb论坛)中,并且效果很好,但是图像嵌入是不正确的(即使它确实可以工作)。

Here's my errors from W3: 这是我在W3中遇到的错误:

required attribute "src" not specified 未指定必需的属性“ src”

required attribute "alt" not specified 未指定必填属性“ alt”

document type does not allow element "img" here 文档类型此处不允许元素“ img”

end tag for "img" omitted, but OMITTAG NO was specified 省略了“ img”的结束标签,但指定了OMITTAG NO

So obviously I did a number on the code, but somehow it's managing to function. 所以很明显,我在代码上做了很多,但是以某种方式设法起作用了。

Here's my hack job of a code, can anyone help me clean this mess? 这是我的代码破解工作,有人可以帮我清理这些混乱吗? (I'm just a newbie, this was the best I could come up with after hours and hours); (我只是一个新手,这是我几个小时后能想到的最好的方法);

<script type="text/javascript"> 

(function ($) {
    $(document).ready(function () {
        $(".content1 .postlink").oembed(null, {embedMethod: "append", maxWidth: 300,});
    });
})(jQuery);

$(document).ready(function () {
    var href = $('a.postlink.img-link').get(0).href;
    $('a.postlink.img-link').html($('').attr('src', href).fadeIn(1000));
    $('a.postlink.img-link').colorbox({rel: 'img-link'});
})
 </script>

Your code could use some tidying up, but we would need to see the relevant HTML to really help you tidy it up. 您的代码可能需要整理一下,但是我们需要查看相关的HTML才能真正帮助您整理一下。

You probably want to change 你可能想改变

$('a.postlink.img-link').html($('').attr('src', href).fadeIn(1000));

to

$('a.postlink.img-link img').attr('src', href)

as a start. 作为开始。

However, to answer your main question about the W3 validator, looks at the page before any javascript has run. 但是,要回答有关W3验证器的主要问题,请在运行任何javascript之前查看页面。 So I am assuming that you have <img> tags with no src attribute (looks like this is getting set by the javascript). 因此,我假设您具有不带src属性的<img>标签(看起来像是由javascript设置的)。 These are the sort of errors that the validator is complaining about. 这些是验证者抱怨的错误。

A HTML page should really 'work' correctly even if javascript is disabled. 即使禁用了javascript,HTML页面也应该正确地“工作”。 Even if this just means showing placeholder content or a message saying that javascript is required. 即使这仅意味着显示占位符内容或显示要求使用javascript的消息。

To fix the validation errors, add src and alt attributes to the img tags in your html, point them at some sort of placeholder images and text that is representative of the actual content that will be replaced by your JS. 要解决验证错误,请将src和alt属性添加到html中的img标签,将它们指向某种占位符图像和文本,这些图像和文本代表将由JS替换的实际内容。

This should also help in the visual rendering of your page. 这也应有助于页面的视觉呈现。

This fixed it. 这样就解决了。

<script type="text/javascript"> //<![CDATA[
    // [your script here]
// ]]></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM