简体   繁体   English

如果href等于另一个div的id,则执行功能

[英]If href equals id of another div do function

I have a hidden div with the details of a thumbnail that is visible on the page. 我有一个隐藏的div,其中包含页面上可见的缩略图的详细信息。 When you click on the thumbnail, it should fadein or slideup the div with details. 单击缩略图时,它应淡入或向上滑动div以显示详细信息。

I have set with jquery incremented ID's to each ".portfolio-item-details" to identify each one and then have set with jquery the same ID's to the href of the thumbnail. 我已经用jquery为每个“ .portfolio-item-details”设置了递增ID以标识每个ID,然后用jquery将相同的ID设置为缩略图的href。

<section id="portfolio1" class="portfolio-item-details hidden">content</section>
<a class="open-portfolio-item-details" href="#portfolio1" title="">
  <img src="thumbnail.jpg">
</a>
<section id="portfolio2" class="portfolio-item-details hidden">content</section>
<a class="open-portfolio-item-details" href="#portfolio2" title="">
  <img src="thumbnail.jpg">
</a>

Since this is done dynamically, how can I with jquery fadeIn or slideUp the ".portfolio-item-details" if the href is equal to the ID. 由于这是动态完成的,如果href等于ID,我该如何使用jquery fadeIn或slideUp“ .portfolio-item-details”。 Basically thumbnail with "#portfolio1" should slide up the div with "#portfolio1" on click. 基本上,带有“#portfolio1”的缩略图应在单击时向上滑动带有“#portfolio1”的div。

This is my jquery code which to add the IDs and HREF is working perfectly but not working to slideUp or fadeIn the div with the same ID. 这是我的jquery代码,用于添加ID,HREF可以正常运行,但不能在具有相同ID的div中对slideUp或fadeIn起作用。

$(document).ready(function () {    
 var i=0;
 $(".portfolio-item-details").each(function(){
      i++;
      var newID="portfolio"+i;
      $(this).attr("id",newID);
      $(this).val(i);
 });
 var i=0;
 $(".open-portfolio-item-details").each(function(){
      i++;
      var newReadMoreHREF="#portfolio"+i;
      $(this).attr("href",newReadMoreHREF);
      $(this).val(i);
      if ($(".portfolio-item-details").attr("id") == "newReadMoreHREF") {
        $(this).fadeIn();
      }
 });
});

SOLUTION

Thanks to Austin's code, I was able to modify it to work with mine. 感谢Austin的代码,我能够对其进行修改以与我的代码一起使用。 Check here: http://jsfiddle.net/jdoimeadios23/xpsrLyLz/ 在这里检查: http : //jsfiddle.net/jdoimeadios23/xpsrLyLz/

The problem is in this block 问题出在这个街区

if ($(".portfolio-item-details").attr("id") == "newReadMoreHREF") {
    $(this).fadeIn();
}

Because you're having two errors, the first one is that newReadMoreHREF is a variable, not a string in your HTML or a value to any variable and so on. 因为有两个错误,所以第一个错误是newReadMoreHREF是变量,而不是HTML中的字符串或任何变量的值,依此类推。

Second thing is, that in the variable declaration you're using "#portfolio"+i; 第二件事是,在变量声明中,您使用的是"#portfolio"+i; , which would be good if you were about to select an element. ,如果您要选择一个元素,那会很好。 But using it in the jQuery iif statement with .attr('id') will again cause a havoc. 但是在带有.attr('id')的jQuery iif语句中使用它会再次造成严重破坏。

The thing that you need is something like this 您需要的是这样的东西

$(".open-portfolio-item-details").each(function(){
  i++;
  var newReadMoreHREF="portfolio"+i; // removed #
  $(this).attr("href",newReadMoreHREF);
  $(this).val(i);
  if ($(".portfolio-item-details a").attr("id") == newReadMoreHREF) {
    // you need to access the hyperlink in the element, not
    // the element itself. this portfolio1 ID is of a hyperlink
    // again here, this is referencing the main iterator. 
    $(this).fadeIn();
    // are you sure you want to fade the .open-portfolio-item-details?
  }
});

Removed the hash sign and then called the variable value to check against. 删除哈希符号,然后调用要检查的变量值。 It would execute to be true if the condition is true. 如果条件为真,它将执行为真。

You want something like this? 你想要这样的东西吗?

HTML 的HTML

<div class="image" value="1">
    <img src="thumbnail.jpg" />
</div>
<div id="portfolio1" class="details">details</div>

<div class="image"  value="2">
    <img src="thumbnail.jpg" />
</div>
<div id="portfolio2" class="details">more details</div>

JS JS

$(document).ready(function () {
    $('.details').fadeOut(1);

    $(".image").click(function () {
        var num = $(this).attr("value");
        $("#portfolio"+num).fadeIn(1000);
    });
});

JSFiddle Demo JSFiddle演示

You don't even need to bother with ID's so long as the next div below each image is it's details. 只要每个图像下面的下一个div是详细信息,您甚至都无需理会ID。

Try this: HTML: content 试试这个:HTML:内容

<section id="portfolio2" class="portfolio-item-details hidden">content</section>
<a class="open-portfolio-item-details" href="#portfolio2" title="">
  <img src="thumbnail.jpg">
</a>

<div id="portfolio1" class="portfolio" hidden>Portfolio 1</div>
<div id="portfolio2" class="portfolio" hidden>Portfolio 2</div>

Jquery: jQuery:

$('a.open-portfolio-item-details').on('click', function (e) {
    e.preventDefault();
    var id = $(this).attr('href');
    $('.portfolio').hide();
    $('.portfolio' + id).fadeIn();
});

Couldn't get the fiddle link for some reason. 由于某种原因,无法获得小提琴链接。

Edit: I don't know the name of the class that shows the content you want displayed, so as an example I'm using portfolio. 编辑:我不知道显示您要显示的内容的类的名称,因此,例如,我正在使用投资组合。 Try putting this code into a fiddle 尝试将这段代码放在小提琴中

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

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