在iFrame中获取标签之间的文本

时间:2014-12-08 01:52:56

标签: javascript jquery html iframe

我正在加载iFrame中的网站。我正在尝试在<carrier_name></carrier_name>之间获取文字,并在文本框内显示该文字。

这是我的HTML

 <iframe name="frameComp" class="frameComp" style="display:none" width="500" height="200"></iframe>

这是我的javascript

 $('button[name="otherlistbut"]').click(function() {
     var phonenum = $('input[name="phonenum"]').val();
     $(".frameComp").fadeIn();
     window.open("https://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=" + "1" + phonenum, "frameComp");

     var message = $('textarea[name="message"]').val();
     message ==  $('.frameComp').contents().find("carrier_name").html();

});

这是网站的源代码

<?xml version="1.0" ?>
<response>
    <results>
        <result item="1">
            <status>OK</status>
            <number>13105555555</number>
            <wless>y</wless>
            <carrier_name>Verizon Wireless</carrier_name>
            <carrier_id>5</carrier_id>
            <country>United States</country>
        </result>
    </results>
</response>

2 个答案:

答案 0 :(得分:0)

好的,不同的方法......

$('button[name="otherlistbut"]').click(function() 
{
    var phonenum = $('input[name="phonenum"]').val();

    $('<iframe id="frameComp" src="https://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=" + "1" + phonenum>').appendTo("body").ready(function()
    {
        // Wait until XML has actually loaded by testing if the required (known) element is there
        var interval = setInterval(function()
        {
            var carrier = $('#frameComp').contents().find("carrier_name")
            if(carrier.length > 0)
            {
                console.log(carrier.text());
                clearInterval(interval);
                $('#frameComp').remove(); // We no longer need the iframe...
            }
        }, 100);
    });
});

希望这有帮助!

答案 1 :(得分:0)

<?php 

$phonenum = $_POST["phonenum"];
$url = "http://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=" . "1" . $phonenum;

$provhtml = file_get_contents($url);
$start_tag = '<carrier_name>';
$end_tag = '</carrier_name>';

$startpos = strpos($provhtml, $start_tag) + strlen($start_tag);
if ($startpos !== false) {
    $endpos = strpos($provhtml, $end_tag, $startpos);
    if ($endpos !== false) {
        $carrier = substr($provhtml, $startpos, $endpos - $startpos);
    }
}
echo json_encode($carrier);

 ?>