javascript
  • html
  • css
  • 2017-02-10 43 views -2 likes 
    -2

    我想改变使用javascript的HTML元素内的另一种风格。如何改变使用javascript内嵌的另一个元素的风格?

    我已经使用下面的代码来改变当前的html元素。

    <p onmouseover="this.style.color = 'black;">This text should be changed</p> 
    <h1>How to change this element when hovered on p element</h1> 
    

    我想使用javascript更改p标记内的其他元素的样式。

    任何人都可以帮忙吗?

    +0

    的可能的复制[添加使用JavaScript内嵌样式(http://stackoverflow.com/questions/14753147/add-inline-style-using-javascript) –

    +0

    与问题不明确... –

    +0

    为什么用js简单的用css'p:hover + h1 {“添加你的风格}' – prasanth

    回答

    0

    这应该是你之后(不是我的工作)是什么 - 检查出小提琴链接...

    <html> 
    <body> 
        <span id="div1" style="color:black;" onmouseover="stext()" onmouseout="htext()">TEXT1</span><p /> 
        <hr color="black" /> 
    <span id="div2" style="color:red;" onmouseover="htext()" onmouseout="stext()">Text2</span> 
    
    </body> 
    

    http://jsfiddle.net/FFCFy/16/

    0

    例如,如果你想改变颜色:

    <script> 
    document.getElementByTagName("p").style.color = "blue"; 
    </script> 
    

    那应该可能绑定到一个事件,相应地你想要做

    1

    使用CSS你可以达到同样的

    <style> 
    p:hover + h1 { 
        background-color : red 
    } 
    </style> 
    
    0

    您可以轻松地用jQuery实现它:

    $(function() { 
        $('#a-element').hover(function() { 
        $('#b-element').css('background-color', 'yellow'); 
        }, function() { 
        // on mouseout, reset the background colour 
        $('#b-element').css('background-color', ''); 
        }); 
    }); 
    

    如果#B#A,最简单的解决方案后,立即来到是在纯CSS:

    #a:hover + #b { 
        background: #ccc 
    } 
    

    如果#a和#b之间是其他元素,你必须使用〜像这样:

    #a:hover ~ #b { 
        background: #ccc 
    } 
    
    0

    使用本:

    <!DOCTYPE html> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <title></title> 
    </head> 
    <body> 
        <p style="color:red;" onmouseover="ch(event)">This text should be changed</p> 
        <h1>How to change this element when hovered on p element</h1> 
    
        <script> 
         function ch(e) { 
          e.target.style.color = "black"; 
          alert(); 
         } 
        </script> 
    
    </body> 
    </html> 
    
    0

    使用Javascript

    function change(that){ 
     
    document.getElementsByTagName('h1')[0].style.color="red"; 
     
        
     
        }
    <p onmouseover="change()">This text should be changed</p> 
     
    <h1>How to change this element when hovered on p element</h1>

    用CSS

    012使用使用

    0
    jQuery("p").mouseover(function(){ 
        jQuery("h1").css("color", "yellow"); 
    }); 
    
    相关问题