2011-12-18 244 views
2

如何更改中元素的background-color? 在我的域中。下面是完整的代码(HTML,PHP和jQuery):在悬停的iframe中更改元素的背景颜色

<?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); 
} 

?> 
<!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> 

    $(function() { 
    var selectedtextbox; 
    $('input[name="myinput"]').focus(function(){selectedtextbox=$(this);}); 
    $('#iframe').load(function(){ 
    $('body *', $(this).contents()).click(function (event) { 
     event.stopPropagation(); 
     event.preventDefault(); 
     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> 
<p> 
    <script type="text/javascript"> 
    document.getElementById('iframe_button').onclick = function() { 
    document.getElementById('iframe').src = '?url=' + document.getElementById('iframe_url').value; 
    }; 
</script> 
    <!-- ... --> 
</p> 
<p>Please click on title 
    <input id="" name="myinput" type="text" value=""> 
</p> 
<p>Please click on Image 
    <input id="" name="myinput" type="text" value=""> 
</p> 
<p>Please click on price 
    <input id="" name="myinput" type="text" value=""> 



</p> 
</body> 
</html> 

回答

3
$("#id_of_element").css("backgroundColor","green"); 

替换id_of_element与你想要的颜色,为改变元素的ID。绿色和它需要改变的颜色。

+0

不,我想实现这个对所有(P,A,李,UL,DIV,IMG,脚本) – 2011-12-18 20:19:32

+2

$(“P,A,李,UL,DIV, IMG “)的CSS(” 的backgroundColor”, “绿色”); – 2011-12-18 20:32:39

+0

不工作! :(...... ... – 2011-12-19 14:12:46

1

用CSS:

#id_of_element:hover { 
    backgroundColor: green 
} 
+0

不,我想对所有的人实现这个(p,a,li,ul,div,img,script) – 2011-12-18 20:19:37

+0

脚本没有物理大小,并且不知道怎么做你需要:所有元素都应该自己改变背景,还是全部改变背景?在第一种情况下,在后一种情况下用'p,a,li,ul,div,img'替换'#id_of_element' ' – SinistraD 2011-12-18 20:38:27

+0

不行!:(我不知道为什么 – 2011-12-19 14:31:11

相关问题