如何将.attr("id")存储在变量中 [英] how to store .attr("id") in a variable

查看:83
本文介绍了如何将.attr("id")存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们以前一直很乐于助人.我一直在搜索stackoverflow已有一段时间了,但是找不到答案.

You guys have been very helpful before. I've been searching stackoverflow for a while now, but can't come up with an answer.

希望这是一个简单的问题.我正在尝试将当前悬停的div的ID存储在一个变量中.然后,我想使用该变量来切换具有相同ID的图像.

Hopefully, this is a simple question. I'm trying to store the id of the currently hovered div in a variable. Then, I want to use that variable to toggle an image with the same id.

您可以看到我已经尝试将变量直接设置为id之一,并且效果很好.我尝试了很多不同的技术.我在做什么错了?

You can see that I've tried setting the variable directly to one of the id's and that works perfectly. I've tried lots of different techniques. What am I doing wrong?

提前感谢您的任何建议!

Thanks in advance for any advice!

我在这里有一个jsFiddle: http://jsfiddle.net/SN3mz/1/

I have a jsFiddle here: http://jsfiddle.net/SN3mz/1/

JQUERY

$(document).ready(function () {
    $('.hover').mouseenter(function() {
    var currentId = $(this).attr("id");
//  var currentId = "#story1";
    $('.pshow').hide();
    $('.feature').find(currentId).toggle();
    });
});

HTML

<div class="row-fluid" id="homepage-spotlight">

<div class="feature" align="center">
    <img class="pshow" id="preview" src="http://i.imgur.com/yuiHc20.png" alt=""/>
    <img class="pshow" id="story1" style="display:none" src="#" />
    <img class="pshow" id="story2" style="display:none" src="#" />
    <img class="pshow" id="story3" style="display:none" src="#" />
    <img class="pshow" id="story4" style="display:none" src="#" />
</div>

<div class="jdlthumbnail">
    <div class="jdlh2"><span>Large Image Features Will Give More Pop.</span>
    <div style="font-size:17px">by Ebenezer Ekuban</div>
</div>

<div class="hover"  id="story1">
    <a href="#"><h2 class="jdlh2b">Story #1 Text<br>Teaser</h2></a>
</div>

<div class="hover"  id="story2">
    <a href="#"><h2 class="jdlh2b">Story #2 Text<br>Teaser</h2></a>
</div>

<div class="hover"  id="story3">
    <a href="h#"><h2 class="jdlh2b">Story #3 Text<br>Teaser</h2></a>
</div>

<div class="hover"  id="story4">
    <a href="#"><h2 class="jdlh2b">Story #4 Text<br>Teaser</h2></a>
</div>

</div>
</div>

推荐答案

也许您需要这样做:

$('.feature').find("#"+currentId).toggle();

这篇关于如何将.attr("id")存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆