使用if条件在jQuery中插入iframe

时间:2014-08-06 18:21:29

标签: jquery html

我有HTML和jQuery的代码。我想在iframe中插入iframe:'您点击了:' ...在if语句中,其中if表示用户单击的状态。

<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
    <script src="jquery-2.1.1.min.js"></script>
    <script src="jquery.usmap.js"></script>
        <title>USA Map</title>

</head>
       <body>
               <div id="map" style="width: 100%; height: 100%;"></div>
               <div id="clicked-state"></div>

         <script>
             $(document).ready(function() {
                 $('#map').usmap({
                     // The click action
                     click: function(event, data) {
                         $('#clicked-state')
                         .text('You clicked: '+data.name)
                         .parent().effect('highlight', {color: '#C7F464'}, 2000);

                    }
                 });
             });
        </script>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用.html代替.text .text('You clicked: '+data.name)

答案 1 :(得分:0)

只需在文档中添加iframe,这样就无需使用javascript插入。

<iframe id="stateFrame" src="default.html" name="stateFrame"></iframe>

关于用户选择的状态,您现在可以在此iframe中加载特定页面。

$(document).ready(function() {
    $('#map').usmap({

    // The click action
    click: function(event, data) {

        // Show some text
        $('#clicked-state').text('You clicked: ' + data.name);

        // Decide which page should be loaded
        if(data.name == "TX") // Texas was selected
        {
            $("#stateFrame").attr("src", "texas.html");
        }
        else if(data.name == "FL") // Florida was selected
        {
            $("#stateFrame").attr("src", "florida.html");
        }
        else // The selected state did not match our creteria, show default page
        {
            $("#stateFrame").attr("src", "default.html");
        }

   }
   });
});

来源:http://snipt.org/XJK7