简体   繁体   English

Alt属性的问题

[英]issue with Alt Attribute

I need help in a jquery code. 我需要有关jquery代码的帮助。 I have developed a website where the header image will change when we hover on the navigation tabs like Home, About Us, etc. The problem is I have included the images that need to changed on hover in "alt" attribute. 我已经开发了一个网站,当我们将鼠标悬停在“首页”,“关于我们”等导航选项卡上时,标题图像将发生更改。问题是我将需要悬停更改的图像包含在“ alt”属性中。 This works fine with chrome but not in firefox and IE. 这适用于chrome,但不适用于Firefox和IE。 The link for the website is 该网站的链接是

            http://datacrawl.in/home.html

HTML: HTML:

<ul>
<li><a href="home.html"> Home  </a><span style = "color: #FFF; position: relative;  left: 23px; "> | </span></li>
<li><a class = "ser" alt = "images/7.jpg" href="about.html"> About Us</a><span style = "color: #FFF; position:             relative; left: 23px;"> | </span></li>
<li><a class = "ser" alt = "images/5.jpg" href=""> Services </a> <span style = "color: #FFF; position: relative; left:     23px;"> | </span>
<ul>
<li style = "background-color: #8e64b0;"><a class = "ser" alt =  "images/3.jpg" href="software.html"> Software             Development </a></li>
<li style = "background-color: #7b55a0;"><a class = "ser" alt = "images/2.jpg" href="web.html"> Web & Graphic              Designing </a></li>
<li style = "background-color: #8e64b0;"><a class = "ser" alt = "images/4.jpg" href="technical.html"> Technical            Training </a></li>
</ul>
</li>
<li><a class = "ser" alt = "images/6.jpg" href=""> Portfolio  </a><span style = "color: #FFF; position: relative;          left: 23px;"> | </span>
</ul>

The jquery code I have used to do this is given below 我用来做的jQuery代码如下

$(document).ready(function () {
    $('.heading1, .heading2, .ser').mouseover(function () {
        var src = $(this).attr('alt');
        $('.header img').stop().fadeOut(50, function () {
            $(this).attr('src', src);
            $(this).fadeIn(50);
        });
    });
    $('.heading1, .heading2, .ser').mouseout(function () {
        $('.header img').stop().fadeOut(50, function () {
            $(this).attr('src', 'images/1.jpg');
            $(this).fadeIn(50);
        });
    });
});

So when ever I mouse over the links with classes heading1, heading2 and ser It will change the header image to respective alt attribute image that is set by me. 因此,每当我将鼠标悬停在带有标题1,标题2和ser类的链接上时,它将标题图像更改为由我设置的相应alt属性图像。 This is not working in firefox and IE. 这在Firefox和IE中不起作用。 Please help me with this. 请帮我解决一下这个。 Thank You. 谢谢。

Good God no! 天哪! Don't use alt to store data. 不要使用alt来存储数据。 Use the data attribute instead. 请改用data属性。 It's what it's designed for. 这就是它的设计目的。

Also, it probably isn't working because you have placed alt onto an anchor tag. 另外,它可能不起作用,因为您已将alt放在了定位标记上。 alt is only used for img tags. alt仅用于img标签。

HTML Reworked: 重做的HTML:

<ul>
    <li><a href="home.html">Home</a><span style = "color: #FFF; position: relative;  left: 23px; "> | </span></li>
    <li><a class = "ser" data-image = "images/7.jpg" href="about.html"> About Us</a><span style = "color: #FFF; position: relative; left: 23px;"> | </span></li>
    <li><a class = "ser" data-image = "images/5.jpg" href=""> Services </a> <span style = "color: #FFF; position: relative; left: 23px;"> | </span>
        <ul>
            <li style = "background-color: #8e64b0;"><a class = "ser" data-image =  "images/3.jpg" href="software.html"> Software Development </a></li>
            <li style = "background-color: #7b55a0;"><a class = "ser" data-image = "images/2.jpg" href="web.html"> Web & Graphic Designing </a></li>
            <li style = "background-color: #8e64b0;"><a class = "ser" data-image = "images/4.jpg" href="technical.html"> Technical Training </a></li>
        </ul>
    </li>
    <li><a class = "ser" data-image = "images/6.jpg" href=""> Portfolio  </a><span style = "color: #FFF; position: relative; left: 23px;"> | </span>
</ul>

jQuery Reworked: jQuery重做:

$(document).ready(function () {
    $('a.ser').mouseover(function () {
        var src = $(this).data('image');
        $('.header img').stop().fadeOut(50, function () {
            $(this).attr('src', src);
            $(this).fadeIn(50);
        });
    });
    $('a.ser').mouseout(function () {
        $('.header img').stop().fadeOut(50, function () {
            $(this).attr('src', 'images/1.jpg');
            $(this).fadeIn(50);
        });
    });
});

Oh god please don't misuse alt tags for this kind of purpose. 哦,上帝,请勿将alt标签用于这种目的。 You can use data attributes for this kind of thing: 您可以将数据属性用于此类操作:

<li><a class="ser" data-header-image="images/3.jpg" href="my-page.html">Link Text</a></li>

Your javascript can stay pretty much the same, but look at that data attribute instead. 您的JavaScript可以保持几乎相同,但是请看该data属性。 Alt attributes hold a semantic implication, so really shouldn't be misused. Alt属性具有语义含义,因此实际上不应该被滥用。 Your code as it stands would be confusing as hell to anyone with a screen reader. 就目前而言,您的代码会使使用屏幕阅读器的人感到困惑。

I also second others' sentiments about making sure that your spacing etc is right and your closing tags are there... That can cause problems, too 我还赞同其他观点,以确保您的间距等正确且您的结束标记在其中...这也会引起问题

更改img源对我不是一个好主意,请尝试制作背景精灵,并通过将类添加到标头中来移动它

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

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