简体   繁体   English

如何使用javascript / jquery获取iframe的URL的锚属性?

[英]How to get the anchor property of a URL of an iframe using javascript/jquery?

I am trying to get the anchor part of a URL from an iFrame using javascript or jQuery. 我正在尝试使用javascript或jQuery从iFrame获取URL的锚点部分。

eg. 例如。

<iframe id="theIFrame" src="location.html#anchorHere"></iframe>

if I use the src attribute I don't get the hash. 如果我使用src属性,则不会获得哈希。

eg. 例如。

$('#theIFrame').attr('src') will return 'location.html' $('#theIFrame').attr('src')将返回'location.html'

How do I get the hash? 我如何获得哈希值?

thank you, George 谢谢乔治

To get everything after the # mark, you can try the following: 要获得#号之后的所有内容,可以尝试以下操作:

var href = $("#theIFrame").attr("href");
var splitHref = href.split("#");
alert(splitHref[1]);

DEMO DEMO

EDIT 编辑

OP was was changed from: OP已从以下位置更改:

<iframe id="theIFrame" href="location.html#anchorHere"></iframe>

to: 至:

<iframe id="theIFrame" src="location.html#anchorHere"></iframe>

so you would need to change the code to: 因此您需要将代码更改为:

var href = $("#theIFrame").attr("src");
var splitHref = href.split("#");
alert(splitHref[1]);

DEMO DEMO

Or , as @Pushpesh has recently noted in the comments section, a more condensed version would be: 或者 ,就像@Pushpesh最近在评论部分中指出的那样,更简洁的版本是:

$('#theIFrame').attr('src').split('#')[1];

Try with 试试看

$('#theIFrame').attr('src').text()

or try 或尝试

$('#theIFrame').attr('href').split('#')[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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