Javascript 如何从对象标记中获取html元素?
我使用object标记在html页面中加载一个html片段 我的代码大致如下:Javascript 如何从对象标记中获取html元素?,javascript,jquery,html,Javascript,Jquery,Html,我使用object标记在html页面中加载一个html片段 我的代码大致如下: 正如预期的那样,页面加载后,在对象标记之间添加了一些元素。 我想获取这些元素,但似乎无法访问它们 我试过以下方法 $(“对象”).html()$(“对象”).children()$(“对象”)[0]。innerHTML 这些似乎都不起作用。有没有其他方法可以获取这些元素 编辑: 一个更详细的例子: 想想这个 如果我试图在对象中获取html,我会得到一个空字符串 试试这个: // wait until object
正如预期的那样,页面加载后,在对象标记之间添加了一些元素。
我想获取这些元素,但似乎无法访问它们
我试过以下方法
$(“对象”).html()
$(“对象”).children()$(“对象”)[0]。innerHTML
这些似乎都不起作用。有没有其他方法可以获取这些元素
编辑:
一个更详细的例子:
想想这个
如果我试图在对象中获取html,我会得到一个空字符串
试试这个:
// wait until object loads
$('object').load(function() {
// find the element needed
page = $('object').contents().find('div');
// alert to check
alert(page.html());
});
下面是一段有效的代码示例。不确定你的代码有什么问题
<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var k = $("object")[0].innerHTML;
alert(k);
$("object")[0].innerHTML = "testing";
});
</script>
<object data="/html_template">hi</object>
</html>
$(文档).ready(函数(){
var k=$(“对象”)[0];
警报(k);
$(“对象”)[0].innerHTML=“测试”;
});
你好
内部html
将提供对介于
和
之间的html的访问。问题是如何让对象在其生成的窗口/框架内加载html(它与打开和关闭标记之间的代码无关)
我也在寻找答案,恐怕没有。如果我找到一个,我会回来把它贴在这里,但我现在正在寻找(而不是独自一人)很多时间。只要你把它放在同一个域名上,你就可以做以下事情:
HTML
当对象数据完全加载且属于同一域时,可以使用以下代码读取对象数据:
HTML-
希望这有帮助 更新
我使用这行Javascript更改iFrame中输入字段的值
document.getElementById('iframeID').contentWindow.document.getElementById('inputID').value='Your value'代码>
参考:
不,无法访问跨原点帧 我知道这是一个老问题,但接下来
我在一个个人网站上使用了它,并最终在一些工作项目中实现了它,但这就是我如何连接到svg的dom中的方法。请注意,您需要在加载对象标记后运行此操作(以便可以使用onload函数触发它)。它可能需要对非svg元素进行自适应
function hooksvg(elementID) { //Hook in the contentDocument of the svg so we can fire its internal scripts
var svgdoc, svgwin, returnvalue = false;
var object = (typeof elementID === 'string' ? document.getElementById(elementID) : elementID);
if (object && object.contentDocument) {
svgdoc = object.contentDocument;
}
else {
if (typeof object.getSVGDocument == _f) {
try {
svgdoc = object.getSVGDocument();
} catch (exception) {
//console.log('Neither the HTMLObjectElement nor the GetSVGDocument interface are implemented');
}
}
}
if (svgdoc && svgdoc.defaultView) {
svgwin = svgdoc.defaultView;
}
else if (object.window) {
svgwin = object.window;
}
else {
if (typeof object.getWindow == _f) {
try {
svgwin = object.getWindow();//TODO look at fixing this
}
catch (exception) {
// console.log('The DocumentView interface is not supported\r\n Non-W3C methods of obtaining "window" also failed');
}
}
}
//console.log('svgdoc is ' + svgdoc + ' and svgwin is ' + svgwin);
if (typeof svgwin === _u || typeof svgwin === null) {
returnvalue = null;
} else {
returnvalue = svgwin;
}
return returnvalue;
};
如果要从dom中获取svg的符号元素,则onload函数可以如下所示:
function loadedsvg(){
var svg = hooksvg('mysvgid');
var symbols = svg.document.getElementsByTagName('symbol');
}
$(“object”)
是否返回某些内容?如果我的猜测正确,您可能正在使用IE浏览器,对吗?是的$(“object”)返回某些内容。来吧,我没有使用IE$('object')。contents()为我$('object')返回[]。contents()是一个集合,您必须在该对象内搜索元素。请尝试使用:$('object').contents().find('html').html();获取null,检查我在上面所做的编辑,即使我在js周围添加setTimeout,仍然无法获取html insidegetting null,因为您的编辑中没有html inside object标记。
<html>
<div class="main">
<object data="/html_template">
</object>
</div>
</html>
$('.main object').load(function() {
var obj = $('.main object')[0].contentDocument.children;
console.log(obj);
});
function hooksvg(elementID) { //Hook in the contentDocument of the svg so we can fire its internal scripts
var svgdoc, svgwin, returnvalue = false;
var object = (typeof elementID === 'string' ? document.getElementById(elementID) : elementID);
if (object && object.contentDocument) {
svgdoc = object.contentDocument;
}
else {
if (typeof object.getSVGDocument == _f) {
try {
svgdoc = object.getSVGDocument();
} catch (exception) {
//console.log('Neither the HTMLObjectElement nor the GetSVGDocument interface are implemented');
}
}
}
if (svgdoc && svgdoc.defaultView) {
svgwin = svgdoc.defaultView;
}
else if (object.window) {
svgwin = object.window;
}
else {
if (typeof object.getWindow == _f) {
try {
svgwin = object.getWindow();//TODO look at fixing this
}
catch (exception) {
// console.log('The DocumentView interface is not supported\r\n Non-W3C methods of obtaining "window" also failed');
}
}
}
//console.log('svgdoc is ' + svgdoc + ' and svgwin is ' + svgwin);
if (typeof svgwin === _u || typeof svgwin === null) {
returnvalue = null;
} else {
returnvalue = svgwin;
}
return returnvalue;
};
function loadedsvg(){
var svg = hooksvg('mysvgid');
var symbols = svg.document.getElementsByTagName('symbol');
}