简体   繁体   中英

jquery Select <a> tag which contains specific href in nested div

how I can select the <a> tag containing the href="www.dummy.ch" in the following code with jquery .

I have to modify the target attribute with _top instead of _blank . No ID or class attribute is set in the tag.

Thanks, Patric

 <div class="QvContent" style="width: 485px; height: 26px; overflow: visible; border-bottom-right-radius: 1px; border-bottom-left-radius: 1px; background-color: rgba(255, 255, 255, 1);"> <div class="QvGrid" style="width: 485px; height: 26px; overflow: hidden; font-family: Tahoma; font-size: 8pt; font-style: normal; font-weight: normal; text-decoration: none; position: relative; cursor: default;" incontainer="false" fixed_cols_left="1"> <a style="position: absolute;" href="http://www.dummy.ch" target="_blank" unselectable="on"> <div title="URL " style="left: 0px; top: 0px; width: 389px; height: 13px; text-align: center; color: rgb(54, 54, 54); overflow: hidden; font-style: normal; font-weight: normal; text-decoration: underline; position: absolute; cursor: pointer; background-color: rgba(255, 255, 255, 1);"> <div style="left: 0px; top: 0px; width: 389px; height: 13px; border-left-color: rgba(220, 220, 220, 1); border-left-width: 0px; border-left-style: solid; position: absolute;" unselectable="on"/> <div class=" injected" style="padding: 0px 2px; width: 385px; height: 13px;"> <div title="URL " style="width: 385px; overflow: hidden; white-space: pre; cursor: default; -ms-word-wrap: normal;" unselectable="on"> <a style="display: inline-block;" href="http://www.axeed.ch" target="_blank" unselectable="on">URL</a> </div> </div> </div> </a> </div> </div> 

尝试

$("a[href*='www.dummy.ch']").attr("target", "_top");

试试看:您可以使用包含 jquery 选择器属性来获取具有href值且包含www.dummy.ch的所有锚,并更改其目标值。

$("a[href*='www.dummy.ch']").attr("target", "_top");

If you want the domain, plus subdomains: $("a[href*='dummy.ch']").attr("target", "_top");

If you want the domain without subdomains: $("a[href*='//dummy.ch']").attr("target", "_top");

If you want to target a specific protocol: $("a[href^='http://dummy.ch']").attr("target", "_top"); (replace "http") with whatever you want.

The *= part suggests that the href attribute contains dummy.ch . The ^= part would mean that the href tag begins with what's inside the quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM