简而言之,我有一个 iframe
从本地 HTML 页面加载并希望访问此页面的元素 通过单击它们
,例如 all type输入
、下拉列表
、表格
等。
这是 iframe 标签内的 HTML 代码:
<!DOCTYPE html>
<html id="bootbody">
<head>
<title>test2</title>
<link href='bootstrap.min.css' type='text/css' rel='stylesheet' />
<style>
@media only screen and (max-width: 767px) and (min-width: 0) {
.panel {
height: auto !important;
}
}
</style>
</head>
<body >
<div id="testttt" class='container text-right'>
<div class='row'>
<div class='panel panel-default pull-right col-xs-12 col-sm-12' style='height:605px' id='16'>
<div class='panel-heading text-right row'>گروه 1</div>
<div class='panel-body'>
<div class='row'>
<div class='col-xs-12 pull-right col-sm-6' style='height:px' id='G1'>
<div class='row'>
<div class='panel panel-default pull-right col-xs-12 col-sm-12' style='height:200px' id='12'>
<div class='panel-heading text-right row'>گروه 2</div>
<div class='panel-body'>
<div class='row'>
<div class='col-xs-12 pull-right col-sm-12' style='height:px' id='G1'>
<div class='row'>
<div class='col-xs-12 col-sm-6 pull-right text-right'>
<div class='row'>
<div class='col-xs-12 col-sm-6 pull-right'><span>تکست 1</span></div>
<div class='col-xs-12 col-sm-6'><input id='layout2' type='text' class='form-control form-control-sm' /></div>
</div>
</div>
<div class='col-xs-12 col-sm-6 pull-right text-right'>
<div class='row'>
<div class='col-xs-12 col-sm-6 pull-right'><span>عنوان</span></div>
<div class='col-xs-12 col-sm-6'><input id='layout17' type='text' class='form-control form-control-sm' /></div>
</div>
</div>
</div>
</div>
<div class='col-xs-12 col-sm-12 pull-right text-right'>
<div class='row'>
<div class='col-xs-12 col-sm-6 pull-right'><span>تکست 2</span></div>
<div class='col-xs-12 col-sm-6'><input id='layout3' class='form-control form-control-sm' type='text' /></div>
</div>
</div>
</div>
</div>
</div>
<div class='panel panel-default pull-right col-xs-12 col-sm-12' style='height:200px' id='13'>
<div class='panel-heading text-right row'>گروه 3</div>
<div class='panel-body'>
<div class='row'>
<div class='col-xs-12 col-sm-12 pull-right text-right'><span>چک باکس 1</span><input id='layout7' type='checkbox'></input></div>
<div class='col-xs-12 col-sm-12 pull-right text-right'><span>چک باکس 2</span><input id='layout8' type='checkbox'></input></div>
</div>
</div>
</div>
</div>
</div>
<div class='col-xs-12 pull-right col-sm-6' style='height:NaNpx' id='G1'>
<div class='row'>
<div class='panel panel-default pull-right col-xs-12 col-sm-12' style='height:200px' id='3'>
<div class='panel-heading text-right row'>گروه</div>
<div class='panel-body'>
<div class='row'>
<div class='col-xs-12 col-sm-12'>
<div class='row'>
<div class='col-xs-12 col-sm-6 pull-right'><span>کومبو1</span></div>
<div class='col-xs-12 col-sm-6'><select id='layout11' class='form-control form-control-sm'></select></div>
</div>
</div>
<div class='col-xs-12 col-sm-12'><button style='height:30px;width:150px' id='btnUpdate_2' class='btn btn-default text-center'>ذخیره 2</button></div>
</div>
</div>
</div>
<div class='col-xs-12 col-sm-12 pull-right text-right'>
<div class='row'>
<div class='col-xs-12 col-sm-6 pull-right'><span>تکست 3</span></div>
<div class='col-xs-12 col-sm-6'><input id='layout12' class='form-control form-control-sm' type='text' /></div>
</div>
</div>
<div class='col-xs-12 col-sm-12'><button style='height:30px;width:150px' id='btnUpdate_1' class='btn btn-default text-center'>ذخیره 1</button></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
然后我将它加载到 asp.net 网络表单中:
<div id="frameWrapper">
<iframe id="frame" src="../HtmlHolder/TEST2.html">[ممکن است بدلیل تنظیمات پیکر بندی مرورگرتان ،قادر به مشاهده ی نمایشگر موبایل نباشید.لطفا مرورگر خود را تغییر دهید.]
</iframe>
</div>
这是访问 iframe 内元素的脚本代码:
<script>
$(document).ready(function () {
var frm = $("#frame").get(0);
var doc = (frm.contentDocument ? frm.contentDocument : frm.contentWindow.document);//here is your document object
var bdy = doc.body;
alert(bdy);
});
</script>
我使用上面的代码找到 iframe 主体,但无法访问它的元素...
我用谷歌搜索它,但发现了其他一些东西,比如 按类名查找元素
或查找 特定元素
对我来说不是这样。
我该怎么做?
非常感谢。
NOTE: I read an HTML code from DB and then upload it into a folder on my host and then view it in ifram tag.
最佳答案
试试这个
jQuery(jQuery('[id="frame"]')[0].contentWindow.document.body).on('click', '*', function() {
console.log("triggered !!");
console.log(this.className); // class of element clicked
});
关于javascript - 通过单击获取 iframe 中的元素类名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36880579/