通过单击获取iframe中的元素类名称

时间:2016-04-27 04:51:14

标签: javascript jquery html iframe

简而言之,我的iframe加载了本地HTML页面并希望访问此页面的by click on them元素,例如all type of inputs,{{ 1}},dropdown list等。
enter image description here

这是iframe标记内的HTML代码:

table

我将其加载到asp.net网络表单中:

<!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>  

这是访问iframe内部元素的脚本代码:

<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> 或找到find element by class name不适合我。   我该怎么做? 非常感谢你。

specific element

3 个答案:

答案 0 :(得分:2)

试试这个

 jQuery(jQuery('[id="frame"]')[0].contentWindow.document.body).on('click', '*', function() {
   console.log("triggered !!");
   console.log(this.className); // class of element clicked
 });

答案 1 :(得分:0)

你可以这样试试。

$(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;
    $(bdy).on("click", "*", function (e) {
        console.log($(e.target).attr("class"));//To get class name
    });
});

答案 2 :(得分:0)

如果有人遇到这些解决方案的问题,我能够使用John R解决方案,但不是使用$(文档).ready我必须听取iframe负载。

像这样:

$("#frame").on('load', function(){
   var frm = $("#frame").get(0);
   var doc = (frm.contentDocument ? frm.contentDocument : frm.contentWindow.document); //here is your document object
   var bdy = doc.body;

   $(bdy).on("click", "*", function (e) {
       console.log($(e.target).attr("class"));//To get class name
   });
});