Javascript 如何使用jQuery访问父元素属性?
如何使用jQuery访问父元素属性?例如,以下代码位于我的网站中:Javascript 如何使用jQuery访问父元素属性?,javascript,jquery,Javascript,Jquery,如何使用jQuery访问父元素属性?例如,以下代码位于我的网站中: <div style="text-align: center;"> <h3>Testing</h3> <p> <a style="display: inline-block;" href="http://www.google.com" target="_blank"> <img class="alignle
<div style="text-align: center;">
<h3>Testing</h3>
<p>
<a style="display: inline-block;" href="http://www.google.com" target="_blank">
<img class="alignleft size-full wp-image-3748" src="/wp-content/uploads/BBB336699.png" alt="BBB" height="45">
</a>
<a style="display: inline-block;" href="http://www.facebook.com" target="_blank">
<img class="alignleft size-full wp-image-3749" src="/wp-content/uploads/COA336699.png" alt="COA" height="45">
</a>
<a style="display: inline-block;" href="http://portal.hud.gov/hudportal/HUD" target="_blank">
<img class="alignleft size-full wp-image-3750" src="/wp-content/uploads/HUD336699.png" alt="HUD" height="45">
</a>
<a style="display: inline-block;" href="http://www.gmail.com/" target="_blank">
<img class="alignleft wp-image-3921" src="/wp-content/uploads/HPF336699.png" alt="HPF" height="45">
</a>
</p>
</div>
当我点击上面的链接时,我得到了一个img
元素事件属性,但我需要a
元素事件属性,比如toElement
有谁能帮我找到这里的主播元素吗。提前谢谢
使用,获取当前匹配元素集中每个元素的父元素(直接父元素),可选地通过选择器进行筛选
$('img')。在('click',函数(e){
//e、 停止传播();
console.log($(this.parent('a').prop('href'));
});代码>
测试
使用,获取当前匹配元素集中每个元素的父元素(直接父元素),可选地通过选择器进行筛选
$('img')。在('click',函数(e){
//e、 停止传播();
console.log($(this.parent('a').prop('href'));
});代码>
测试
问题是因为您正在使用窗口事件。改为使用此
引用单击的元素:
$("a").click(function(e) {
e.preventDefault(); // stop the page being redirected
var re = /speridian/gi;
if ($(this).prop('href').search(re) == -1) {
alert("it is not");
}
});
问题是因为您正在使用窗口.event
。改为使用此
引用单击的元素:
$("a").click(function(e) {
e.preventDefault(); // stop the page being redirected
var re = /speridian/gi;
if ($(this).prop('href').search(re) == -1) {
alert("it is not");
}
});
$('img')。在('click',function(){$(this).parent('a');})
上,为什么不直接将事件处理程序挂接到a
而不是img
?Hi@RoryMcCrossan,我尝试了以下方法:HTML:JS:$('a”)。单击(function(event){var key=window.event.toElement var-re=/speridian/gi;if(key.href.search(re)==-1{警报(“它不是”);});这里是IMG事件属性使用单击处理程序中的this
引用单击的元素Hi@rorymcrossan,任何示例请$('IMG')。在('click',function(){$(this).parent('a');})
上,为什么不直接将事件处理程序挂接到a
而不是IMG
?Hi rorymcrossan,我尝试了以下方法:HTML:JS:$(“a”).click(函数(事件){var key=window.event.toElement var re=/speridian/gi;if(key.href.search(re)==-1){alert(“它不是”);});这里是IMG事件属性使用单击处理程序中的this
引用单击的elementHi@RoryMcCrossan,请提供任何示例当单击上面的链接时,我如何获取href值?当单击上面的链接时,我如何获取href值?谢谢,它对我来说工作正常!再次感谢没问题,很高兴能帮上忙谢谢,它对我来说很好!再次谢谢你没问题,很乐意帮忙