0

通过 javascript 访问维基百科信息框时我的错误在哪里?
(我正在使用 codeigniter 框架)

页面控制器

    $data = array(
               'title' => 'My Title',
               'country' => 'My Heading'
          );

    $countrycodes = ['au', 'us'];
    $countrynames = ['Australia', 'United_States'];
    error_log(print_r($page, TRUE)); 
    $key = array_search($page, $countrycodes);
    error_log(print_r($key, TRUE)); 
    if($key !=null){
    $data['country'] = $countrynames[$key];
    }

    $this->load->helper('url');
    $this->load->helper('utility');

    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);
    if(!is_null($key)){
    $this->load->view('templates/infobox', $data);  
    }
    $this->load->view('templates/footer', $data);

非盟页面

...

<div style =" position:absolute;top:90%; background: red;">
<?php echo $country; ?></div> // prints Australia.
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/country.js"></script>
// variable isnt formed yet to be picked up  by javascript.

信息框模板

<div class='info-box' id="<?php echo $country; ?>">
    <table id ="result">
</div>

国家/地区 JavaScript

$(window).load(function(){
        var searchTerm= $(".info-box").attr("id");

        alert(searchTerm);
        //searchTerm= "Australia";

        var url="http://en.wikipedia.org/w/api.php?action=parse&format=json&page=" + searchTerm+"&redirects&prop=text&callback=?";
        $.getJSON(url,function(data){
          wikiHTML = data.parse.text["*"];
          $wikiDOM = $("<document>"+wikiHTML+"</document>");
          $("#result").append($wikiDOM.find('.infobox').html());
        });
});

我也试过 body onload = "function();"

浏览器源码

4

1 回答 1

0

使用主体 onload = "function();" 确实有效。我想我忘了放一条线,然后跳了枪。

function loadWiki(){
        var searchTerm= $(".info-box").attr("id");

        var url="http://en.wikipedia.org/w/api.php?action=parse&format=json&page=" + searchTerm+"&redirects&prop=text&callback=?";
        $.getJSON(url,function(data){
          wikiHTML = data.parse.text["*"];
          $wikiDOM = $("<document>"+wikiHTML+"</document>");
          $("#result").append($wikiDOM.find('.infobox').html());
        });
    }
于 2013-08-17T08:58:12.557 回答