jQuery - 从图像中获取标题并将其放在单独的标签上(每个标签)
$(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>
总而言之,代码已经生成了a标签的所有标题,但只生成了第一个标题并将其应用于所有a标签,我需要每个标签的标题整个页面的图像应用到特定区域的 a 标签。
有什么想法吗?
$(function () {
var ptitle = $('div.portfolioItem div.image img').attr("title");
$(this).each(function () {
$('a.play').attr({
'title': '' + ptitle + ''
});
});
});
Well there's my code, it works but I'm not really familiar with the each tag.
All this does currently is applies the first 'ptitle' to all of the a.play tags.
What I'm trying to achieve is to get the title from each image and apply it to only the relevant a.play button.
Here's the html if this is of any help to understand:
<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>
To sum it up, the code already generates all the titles to the a tag, but only generates the first title and applies it to all the a tags, I need the titles of each image throughout the page to apply to the a tag in the specific area.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可能正在迭代错误的元素。
听起来您可能想要迭代每个投资组合图像。
从图像中获取标题。
查找带有类图像的 div 以找到包含的播放类
然后应用该属性。
I think you may be iterating over the wrong element.
It sounds like you might want to iterate over each of the portfolio images.
Grab the title from the image.
Look for the div with class image in order to find the contained play class
Then apply the attribute.
会找到你的每一张图像并迭代每一张。每次迭代都会获取父项(在本例中为 div.info)并找到其中的任何 a.play 并将该标题设置为图像标题。请注意,匹配集“a.play”上的 attr( ) 只会在第一个匹配项上运行——假设每个图像只有一个播放按钮,这样就可以了。
您可能还需要考虑为所有原始图像提供一个类,以便您可以将代码更改为以下内容:
或者,您可以这样做:
它将找到每个播放链接,然后搜索适当的图像(而不是查找图像并搜索播放链接)。同样,这假设您将“mybutton”类添加到图像中。
Would find each of your images and iterate through each one. Each iteration would get the parent item (in this case the div.info) and find any a.play within it and set that title to the images title. Note that attr( ) on the matched set "a.play" will only run on the first matched item--assuming only one play button per image and this would be fine.
You may also want to consider giving the original images all a class so that you could change the code to something like:
Alternately you could do this:
Which would instead find each of the play links and then search for the appropriate image (instead of finding the image and searching for the play links). Again this would be assuming you added the "mybutton" class to the images.