javascript
  • html
  • css
  • 2017-02-10 44 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"); 
    }); 
    
    相關問題