在iframe中获取元素的xpath位置(来自我的域的iframe)

时间:2011-12-15 18:08:15

标签: php jquery iframe xpath textfield

我有两个代码,我尝试以某种方式连接这两个代码。在代码中也是获取xpath位置的函数,但是如何在iframe上使用它。 $(#iframeID) ....并将getXpath函数的结果写入焦点字段。首先我加载一些URL我尝试使用getXpath位置函数并将结果写入焦点的字段但不起作用。

以下是我用来获取内容的一些代码,并在我的域名中显示iframe的代码(因此我可以获取XPath因为iframe在我的域中),然后使用getXpath函数将结果写入焦点领域:

<?php 

error_reporting(E_ALL ^ E_NOTICE); 

$url = $_GET['url']; 

if( ! empty($url)) 
{ 
    $data = file_get_contents($url); 

    $data = str_replace('<head>', '<head><base href="'.$url.'" /></base>', $data); 

    $data = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $data); 
    $data = preg_replace('#<iframe(.*?)></iframe>#is', '', $data); 

    $data .=  
    ' 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script> 
    $("div").each(function(i){ 
        if($(this).css("position") == "fixed") $(this).css("display", "none"); 
    }); 
    </script> 
    ' 
    ; 

    die($data);  
} 

?> 

这是HTML / JavaScript:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <meta name="author" content="Webarto" /> 

    <title>AdriaMart</title> 

<script src="http://code.jquery.com/jquery-latest.js"></script> 
//function for xpath location
<script type="text/javascript">
    function getXPath(node, path) {
        path = path || [];
        if(node.parentNode) {
          path = getXPath(node.parentNode, path);
        }

        if(node.previousSibling) {
          var count = 1;
          var sibling = node.previousSibling
          do {
            if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {count++;}
            sibling = sibling.previousSibling;
          } while(sibling);
          if(count == 1) {count = null;}
        } else if(node.nextSibling) {
          var sibling = node.nextSibling;
          do {
            if(sibling.nodeType == 1 && sibling.nodeName == node.nodeName) {
              var count = 1;
              sibling = null;
            } else {
              var count = null;
              sibling = sibling.previousSibling;
            }
          } while(sibling);
        }

        if(node.nodeType == 1) {
          path.push(node.nodeName.toLowerCase() + (node.id ? "[@id='"+node.id+"']" : count > 0 ? "["+count+"]" : ''));
        }
        return path;
      };

</script>

//function to get  xpath location and write in textfield in focus
<script type="text/javascript">

    $(document).ready(function () {
        var selectedtextbox;
$('input[name="myinput"]').focus(function(){selectedtextbox=$(this);});
        $('p, li, a, href').click(function () { 
           var xpath = getXPath(this);
            selectedtextbox.val(xpath)
        });
    });
</script>
<style type="text/css"> 
<!-- 
iframe{width:100%;height:400px;} 
--> 
</style> 

</head> 
<body> 

<!-- ... -->
<input id="iframe_url" name="" type="text" />
<input id="iframe_button" name="" type="button" />
<iframe id="iframe" src="?url=http://kupime.com"></iframe>
<script type="text/javascript">
  document.getElementById('iframe_button').onclick = function () {
    document.getElementById('iframe').src = '?url=' + document.getElementById('iframe_url').value;
  };
</script>
<!-- ... -->
//Textfields for xpath location to write inside them
<input id="" name="myinput" type="text" value="">
<input id="" name="myinput" type="text" value="">
<input id="" name="myinput" type="text" value="">



</body> 
</html>

0 个答案:

没有答案