如何使用javascript更改另一个内联元素的样式?
我想使用javascript更改html元素内部另一个元素的样式 我使用了下面的代码来更改当前的html元素如何使用javascript更改另一个内联元素的样式?,javascript,html,css,Javascript,Html,Css,我想使用javascript更改html元素内部另一个元素的样式 我使用了下面的代码来更改当前的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> 应更改此文本 将鼠标悬停在p元素上时如何更改此元素 我想使用j
<p onmouseover="this.style.color = 'black;">This text should be changed</p>
<h1>How to change this element when hovered on p element</h1>
应更改此文本
将鼠标悬停在p元素上时如何更改此元素
我想使用javascript更改p标记中另一个元素的样式
有人能帮忙吗?这应该是你想要的(不是我的工作)-查看小提琴链接
<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>
TEXT1
文本2
例如,如果要更改颜色:
<script>
document.getElementByTagName("p").style.color = "blue";
</script>
document.getElementByTagName(“p”).style.color=“蓝色”;
这可能会绑定到一个事件,相应地,您可以使用CSS实现相同的操作
<style>
p:hover + h1 {
background-color : red
}
</style>
p:悬停+h1{
背景颜色:红色
}
使用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
}
使用以下命令:
<!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>
应该更改此文本
将鼠标悬停在p元素上时如何更改此元素
功能ch(e){
e、 target.style.color=“黑色”;
警惕();
}
与Javascript一起使用
功能更改(即){
document.getElementsByTagName('h1')[0].style.color=“red”;
}
应更改此文本
当悬停在p元素上时如何更改此元素可能重复的不清楚的问题…为什么将js simple use与css一起使用
p:hover+h1{“添加样式”}
jQuery("p").mouseover(function(){
jQuery("h1").css("color", "yellow");
});