简体   繁体   English

单击另一个对象时如何显示跨度

[英]How to show spans when another object is clicked

Here is my HTML:这是我的 HTML:

<div id="sidebar">

    <table id="table1">

        <tr>
            <th>Charities</th>
        </tr>

        <tr>
            <td>
                <a rel="group1" class="links">Age UK</a>
            </td>
        </tr>

        <tr>
            <td>
                <a rel="group2" class="links">Brainstrust</a>
            </td>            
        </tr>
    </table>
</div>

<div id="box">
    <img alt="" src="image1.png" id="group1" class="groups"/>
    <img alt="" src="image2.png" id="group2" class="groups"/>


<span id="group1" class="groups">1st span</span>
<span id="group2" class="groups">2nd span</span>

And my jQuery:还有我的 jQuery:

$( window ).load(function() {
    $(".groups").hide()
    $('a').click(function(){
        var rel = $(this).attr('rel');
        $('a').removeClass('active');
        $(this).addClass('active');
        $(".groups").hide()  
        $('#'+rel).fadeIn('slow');
    });
});

When I click on one of the td's the corresponding image shows.当我单击 td 之一时,会显示相应的图像。 However, all spans are remaining hidden when the first should be visible when the first td is clicked & the same with the second.但是,当单击第一个 td 时第一个应该可见时,所有跨度都保持隐藏状态,与第二个相同。 Why are the spans not becoming visible?为什么跨度不可见?

The .groups is working in the javascript, as nothing is visible when I open the webpage. .groups 正在 javascript 中工作,因为当我打开网页时什么都看不到。 However, when I click on one of the td's, the image shows, but not the span to go with it.但是,当我单击其中一个 td 时,会显示图像,但不会显示与之对应的跨度。 I think this means that the id of the span is not working with the fadeIn part of the jQuery.我认为这意味着跨度的 id 不适用于 jQuery 的淡入淡出部分。 What can I do to make the #+rel be the same as the span id?我该怎么做才能使 #+rel 与 span id 相同? It looks as though they should be equal, so I don't know if I'm right in saying that it's not linking correctly - just my best guess.看起来它们应该相等,所以我不知道我说它没有正确链接是否正确 - 只是我最好的猜测。

There is nothing in your code to make the spans visible.您的代码中没有任何内容可以使跨度可见。 Add an id to each span, preferably something like "txtimg1" and "txtimg2".为每个跨度添加一个 id,最好是“txtimg1”和“txtimg2”之类的。 Then in your jQuery you can reference it in a similar way to what you are doing with the image.然后在您的 jQuery 中,您可以以与您对图像所做的类似的方式引用它。

$('#txt'+rel).fadeIn('slow');

I can't see any javascript that is hiding/displaying the spans anywhere in your code.我在您的代码中的任何地方都看不到任何隐藏/显示跨度的 javascript。

You could alter the HTML like this:您可以像这样更改 HTML:

<div id="box">
    <div id="img1">
        <img src="cant-believe-it-icon.png" alt="" />
        <span>Text with fist image</span>
    </div>
    <div id="img2">
        <img src="too-much-icon.png" alt="" />
        <span>Text with second image</span>
    </div>
</div>

Or something like this:或者像这样:

<a rel="group1" class="links">Age UK</a>

<div id="box">
    <img alt="" src="image1.png" id="group1" class="groups" />
    <img alt="" src="image2.png" id="group2" class="groups" />

    <span id="group1_text" class="groups">1st span</span>
    <span id="group2_text" class="groups">2nd span</span>
</div>

And

$(".groups").hide()
$('a').click(function(){
    var rel = $(this).attr('rel');
    $('a').removeClass('active');
    $(this).addClass('active');
    $(".groups").hide()  
    $('#' + rel).fadeIn('slow');
    $('#' + rel + '_text').fadeIn('slow');
});

You maybe a newbie to the jQuery here that's why you're making small mistakes and not a blunder.您可能是 jQuery 的新手,这就是为什么您会犯小错误而不是失误的原因。 Let me explain the code for you.让我为您解释代码。

Here is the code that you're using.这是您正在使用的代码。 It doesn't work, and even it doesn't hide the elements on the start up.它不起作用,甚至它不会在启动时隐藏元素。

http://jsfiddle.net/afzaal_ahmad_zeeshan/ekh2g/ http://jsfiddle.net/afzaal_ahmad_zeeshan/ekh2g/

The initial state of your code.代码的初始状态。

First Mistake第一个错误

The very first mistake that you're doing has been talked about in posts million of times.你犯的第一个错误已经在帖子中被讨论了数百万次。 Sorry if you never knew that.对不起,如果你从来不知道。 But, you need to understand that the jQuery code must be in a document load event, not the window load event.但是,您需要了解 jQuery 代码必须在文档加载事件中,而不是在窗口加载事件中。

Something like this:像这样的东西:

$(document).ready(function () {
  /* all of the code here */
});

Event is handled now现在处理事件

Now, when I removed the $(window).load(function () { ... }) you can see the spans were hidden below.现在,当我删除$(window).load(function () { ... })您可以看到跨度隐藏在下面。

http://jsfiddle.net/afzaal_ahmad_zeeshan/ekh2g/1/ http://jsfiddle.net/afzaal_ahmad_zeeshan/ekh2g/1/

I have commented out your code and then used an alert to detect the function working.我已注释掉您的代码,然后使用警报来检测功能是否正常工作。 And it was working correctly and perfectly.它工作正常且完美。

Removed the comments;删除了评论; it fadeIn now现在淡入

Now I have removed the comments around your code.现在我已经删除了围绕您的代码的注释。 And now once you click on the hyperlinks.现在一旦你点击超链接。 You will see the elements fading in.您将看到元素逐渐淡入。

That is what the issue was.这就是问题所在。 here is the fiddle: http://jsfiddle.net/afzaal_ahmad_zeeshan/ekh2g/2/ You will see, that now the code is working as it's been told to do.这是小提琴: http : //jsfiddle.net/afzaal_ahmad_zeeshan/ekh2g/2/你会看到,现在代码正在按照要求的那样工作。

The main issue was the event handler for the code.主要问题是代码的事件处理程序。 Which must be always a document ready function.这必须始终是文档就绪功能。

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

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