使用jQuery在Android WebView中读取/写入iframe内容

时间:2014-10-11 01:53:04

标签: android jquery android-webview

我正在html上加载本地Android WebView文件,并根据我的期望加载/显示,但由于某些原因,jQuery无法写入/读取内容一个<iframe>元素。

主要HTML:

<!DOCTYPE html>

<html>

<head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" type="text/css" href="css/style-test.css" />
    <title> WebTutee Quiz </title>
</head>

<body>
    <div id="wrapper">
        <div id="content">
            <h1 id="quiz-title" > WebTutee <span class="text-green">HTML Quiz</span> </h1>
            <h4>HTML Quiz</h4>

            <div id="frame-page">
                <iframe id="quiz-page"
                        src="pages_html/1.html"
                        style="width:100%; height: 280px; border: 0px; overflow: auto;" >
                </iframe>
            </div>

            <div id="page-navigator">
                <input type="button" value="Next" />
                <input type="hidden" id="current-page" value="1" />
            </div>
        </div>
    </div>
    <script src="js/jquery-2.1.1.min.js"></script>
    <script src="js/html-query.js"></script>
</body>

</html>

iframe html

<!DOCTYPE html>

<html>

<head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" type="text/css" href="../css/style-test.css" />
</head>

<body>
    <div id="page-content">
        <div id="quiz-content">
            <p id="quiz-question">
            1. What does HTML stand for?
            </p>
            <div id="quiz-selections">
                <ul>
                    <li>
                        <label>
                            <input type="radio" name="quiz-answer" class="quiz-answer"/>
                            Home Tool Markup Language
                        </label></li>
                    <li>
                        <label>
                            <input type="radio" name="quiz-answer" class="quiz-answer"/>
                            Hyper Text Markup Language
                        </label></li>
                    <li>
                        <label>
                            <input type="radio" name="quiz-answer" class="quiz-answer"/>
                            Hyperlinks and Text Markup Language
                        </label></li>
                </ul>
            </div>
        </div>
    </div>
</body>

</html>

的jQuery

function retrieveAnswer() {

    var selections = $('#quiz-page').contents().find('.quiz-answer');

    var hasAnswer = false;

    alert( selections.length );
    $(selections).each( function(index, obj) {
        if ( $(this).is(':checked') ) {
            input_answers.push( index + 1 );
            hasAnswer = true;
            alert('has answer!');
        }
    });

    if ( !hasAnswer ) {
        input_answers.push( 0 );
    }
}

爪哇

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_category);

    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.select);
    mp.start();

    WebView theWebPage = new WebView(this);
    setContentView(theWebPage);
    theWebPage.setWebChromeClient( new WebChromeClient() );
    theWebPage.getSettings().setJavaScriptEnabled( true );
    theWebPage.loadUrl("file:///android_asset/test_html.html"); 

}

html文件在我的PC的Firefox中工作正常,但jQuery读/写无法完成任务。

我正在考虑一个事实,我的html文件在Chrome中无效,因为same origin policy w / c意味着它无法访问<iframe>所指的文件但是此代码在Android WebView中有效,但在PC的Google Chrome中无效:

function loadPage(page) {
    $('#quiz-page').attr('src', "pages_html/" + page + ".html");
}

其中#quiz-page<iframe>的ID。

0 个答案:

没有答案