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

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

问题描述

你们以前非常乐于助人.我已经在 stackoverflow 上搜索了一段时间,但找不到答案.

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

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

提前感谢您的建议!

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

查询

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

HTML

<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 class="jdlthumbnail"><div class="jdlh2"><span>大图像特征将带来更多流行.</span><div style="font-size:17px">作者:Ebenezer Ekuban</div>

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

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

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

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

解决方案

也许你需要这样做:

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

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

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.

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!

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>

解决方案

Maybe you need to do:

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

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

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆