html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
.home{
width: 800px;
height: 300px;
}
textarea{
width: 500px;
height: 30px;
resize: none;
box-sizing: border-box;
}
.father{
width: 500px;
height: 35px;
border-radius: 5px;
border: 1px solid rgb(6, 6, 112);
}
.son{
/* float: left; */
border-radius:5px 0px 0px 5px ;
width: 50px;
height:35px ;
background-color: rgb(87, 87, 250);
text-align: center;
}
.home .father .son span{
line-height: 35px;
color: white;
}
style>
head>
<body>
<div class="home">
<textarea name="" id="" cols="30" rows="10">textarea>
<div class="father">
<div class="son">
<span>10%span>
div>
div>
div>
<script>
let ip = document.querySelector('textarea')
father = document.querySelector('.father')
son = document.querySelector('.son')
tex = document.querySelector('span')
ip.addEventListener('input',function(){
son.style.transition = '0.6s'
son.style.width = `${ip.value*5}px`
son.innerText = `${ip.value}%`
})
script>
body>
html>
因为你把 span 干掉了
// son.innerText 改成下边这个
son.innerHTML = `<span>${ip.value}%</span>`
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
在你的代码中,.home .father .son span选择器设定了line-height和color属性。当你更改son的宽度时,实际上是更改了span所在的父元素.son的宽度,因为你在JavaScript中更改了.son的width属性。这导致.son的宽度会自动调整以适应其子元素span的大小,所以span的line-height和color属性也会跟着更改。
解决方案是:将line-height和color属性设置在span上,而不是其父元素.son上,这样即使更改.son的宽度,也不会影响span的样式。
具体代码改动如下:
.home .father .son span{
line-height: 35px;
color: white;
}
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢