$(function () {
    var ptitle = $('div.portfolioItem div.image img').attr("title");
    $(this).each(function () {
        $('a.play').attr({
            'title': '' + ptitle + ''
        });
    });
});

那么有我的代码,它的工作原理,但我不是很熟悉的每一个标签。

这一切都目前是施加第一“ptitle”到所有a.play标签。

我试图做到的,是从每个图像拿到冠军,并只适用于相关的a.play按钮。

这里的HTML,如果这是任何帮助理解:

<figure class="oneThird">
    <div class="portfolioItem">
        <div class="image">
            <!-- This is the image -->
            <img src="images/portfolio-item-2.jpg" alt="portfolio item" title="test2" />
            <div class="info">
                <p> Alright, go to the link Andrew propose. Download the file. 
                    You need to add it to your project. You can still have 
                    jquery.effects.core this is why this is random fill to 
                    take space.
                </p>
                <span class="base">
                    <!-- Add the img title to the <a class="play"> below -->
                    <a href="images/header-image-2.jpg" rel="prettyPhoto[portfolio]" class="play"></a>
                    <a href="#" class="view">View Project</a>
                </span>
            </div>
        </div>
    </div>
    <h3>
        <a href="#">This is a portfolio item</a>
    </h3>
    <ul class="tags">
        <li><a href="#">Web</a>, </li> 
        <li><a href="#">Print</a>, </li> 
        <li><a href="#">Design</a></li> 
    </ul> 
</figure>

要概括起来讲,代码已经生成的所有标题的标签,但只产生了第一个冠军,并适用于所有的一个标签,我需要在整个页面的每个图像的标题应用到一个在该特定区域的标签。

任何想法?

有帮助吗?

解决方案

我想你可能是遍历错误的元素。

这听起来像你可能要遍历每个组合图像。 抓住从图像标题。 为了找到所包含的游戏类寻找具有类图像的DIV 然后应用该属性。

$('div.portfolioItem div.image img').each( function(idx,img) {
    var pTitle = img.title;
    $('a.play', $(img).parent('.image')).attr('title',pTitle);
});

其他提示

$('div.portfolioItem div.image img').each(function () {
    $(this).parent().find('a.play').attr('title', $(this).attr('title'));
});

会发现每个图像,并通过迭代每一个。每次迭代会得到父项(在这种情况下div.info),并发现其中的任何a.play,并设置标题的图片标题。注意,ATTR()的匹配组“a.play”将仅在第一个匹配的项目上运行 - 假定只有一个每图像播放按钮,这将是细

您可能还需要考虑给原始图像的所有类,让你可以将代码更改为类似:

$('img.mybutton').each(function () {
    $(this).parent().find('a.play').attr('title', $(this).attr('title'));
});

或者你可以这样做:

$('a.play').each(function () {
    this.title = $(this).parents('div.image').find('img.mybutton').attr('title');
});

他反而觉得每次播放链接,然后寻找合适的图像(而不是找到的图像和搜索播放链接)。同样,这将被假定添加的“myButton的”类的图像。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top