如何使用jquery触发父元素

时间:2018-03-20 03:06:31

标签: jquery

我有这个HTML

<div class="gallery_images" id="10">
    <a href="" data-toggle="modal" data-target="#myModal9" tabindex="0">
        <img class="img-fluid" src="http://contest.geojidesign.com/wp-content/uploads/contest_entries/thumbnail-1680__1050__auto___wp-content_uploads_2015_01_ajax-forms.jpg">
    </a>
</div>

网页的网址为http://v9contest.geojidesign.com/?img=http://v9contest.geojidesign.com/wp-content/uploads/contest_entries/1680__1050__auto___wp-content_uploads_2015_01_ajax-forms-1.jpg

使用jquery我想从页面url获取图像链接,如果与html中的图像url匹配,则触发作为锚标记的图像父图像上的点击事件。

1 个答案:

答案 0 :(得分:0)

使用URLSearchParams获取url参数和trigger事件以触发点击。

&#13;
&#13;
var searchParams = new URLSearchParams(window.location.search)
var param = '';
if(searchParams.has('img')) // true
{
    param = searchParams.get('img')
}
var imgSrc = $('.img-fluid').attr('src');

if (param==imgSrc)
{
   $( '.img-fluid' ).parent().trigger( "click" );
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery_images" id="10">
    <a href="" data-toggle="modal" data-target="#myModal9" tabindex="0">
        <img class="img-fluid" src="http://contest.geojidesign.com/wp-content/uploads/contest_entries/thumbnail-1680__1050__auto___wp-content_uploads_2015_01_ajax-forms.jpg">
    </a>
</div>
&#13;
&#13;
&#13;