使用Javascript(或jQuery)加载iframe内容

时间:2015-03-29 17:24:57

标签: javascript jquery html iframe

我需要读取一个外部代码,该代码来自html文件的同一文件夹中的文件。问题是HTML没有在服务器上运行,所以我不能使用jQuery load()和其他解决方案。所以我认为通过这样的iframe加载代码:

<iframe id="iframe" src="script1.html"></iframe>

文件中的文字是这样的:

One paragraph. Some words
Other paragraph, and more words...

我需要获取句子并抛出一个数组,但我甚至无法通过jQuery获取文本。我正在尝试这样做:

var text = $("#iframe").val();

但它没有做任何事情,所以我尝试在文件中插入html标签:

$('#iframe').contents().find("html").html();

再也没有了。那么,怎么做呢?

2 个答案:

答案 0 :(得分:0)

您是否尝试使用简单的$ .ajax发布/接听电话?而不是.load,.ajax将处理任何JS&amp; script1.html中的HTML

某些人喜欢:

$.ajax({ type: "POST", url:"script1.html", dataType:"html", success: function(data) { // do what you want here }});

巴普蒂斯特

答案 1 :(得分:0)

您可以通过以下方式获得您的价值:

 var iBody = $("#iframe").contents().find("body");

//here you have the control over your element (#myContent)
var myContent = iBody.find("#myContent"); //store it in variable here where myContent is the div id you want to get value of..