jQuery 从特定子项获取链接
jQuery obtain links from specific child
可以做以下事情吗?
我有这个div:
<div class="articleBoxContent">
<h2><a href="myLink">Title</h2>
<p>
<a href="anotherLink">
<img src="img.jpg">
</a>
</p>
<p>My text is so awesome! I like ponies and cookies.</p>
</div>
以及以下 jQuery 代码段:
$(".articleBoxContent").click(function() {
window.location = $(this).find("h2").find("a").attr("href");
return false;
});
我想做的是,当我单击我的div 时,只有当div 在 p 元素中不包含另一个链接时,它才会将我发送到 H2 元素 (myLink) 中包含的链接。
但是我不知道如何对jQuery说将H2元素中的链接与p元素中的链接区分开来。知道吗?
谢谢!
使用:
$(".articleBoxContent").click(function() {
var el = $(this);
if( el.find('p a[href]').length == 0 ){
el.find('h2 a[href]').trigger('click');
} // if
});
在p
元素内部进行检查:
$(".articleBoxContent").click(function() {
if ($(this).find("p a").length) {
//Found some anchor elements inside a "p" tag, do nada
return false;
} else
//Didnt find an anchor inside the "p" tags - redirect
window.location = $(this).find("h2").find("a").attr("href");
}
});
试试看:
$(".articleBoxContent").click(function(e) {
e.preventDefault();
e.stopPropagation();
var $this = $(this);
if(!$this.find('p a').length) {
window.location = $this.find("h2 a").attr("href");
}
return false;
});
相关文章:
- AngularJS:获取链接中没有哈希的查询字符串值
- angular.js从数据库中获取链接
- jQuery 从特定子项获取链接
- 如何获取链接以保持活动状态
- Javascript的Haskell处理“;点击“;以获取链接
- 如何获取链接内容(innerHTML)并将其作为标题添加到其他链接
- 正在从随机文章中获取链接
- 动态生成id时获取链接的id
- 从h1标签获取链接并使图片可下载
- 使用jQuery获取链接的属性,并将其作为表单发布到另一个脚本文件
- 使用 JavaScript 从链接列表中获取链接名称
- 获取链接帖子使用脚本“data:post.url”博主不起作用
- 在IE 7中,如果链接是使用jQuery创建的,如何获取链接的href属性的文字值
- 如何从数组获取链接以在youtube Iframe API中播放
- jQuery:获取链接的html代码
- 单击时获取链接的 ID
- 混合面板 - 跟踪所有链接并获取链接值
- 如何使用 Scrapy 从点击事件获取链接
- 如何使用 Python 和 Selenium 获取链接的点击“操作”
- 如何在没有图像的情况下获取链接