【我不熟悉的html】span标签使用innerText获取内容有多余的\n,绝对定位的元素影响innerText的值

结论: 如果span标签内,含有相对(fixed,absolute)定位的span标签,使用innerText获取内容的时候,会有多余的\n

话不多说,直接看效果

 所以使用innerText的时候,要先去除无用的绝对定位的元素!!

<!DOCTYPE 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>Document</title>
    <script src="./dist/bundle.js"></script>
    <style>
        .inline-block {
            display: inline-block;
            color: red;
        }
        .para-wrapper {
            position: relative;
            display: inline-block;
        }
        .absolute-para {
            position: absolute;
            left:0;
            top:0
        }

        .fixed-para {
            position: absolute;
            left:0;
            top:0
        }
    </style>
</head>
<body>
    <div id="example1">例子1: 今天是个好日子<p class="inline-block">段落</p>你在干什么</div>

    <br/>
    <div id="example2">例子2: 今天是个好日子<div class="inline-block">段落
        <p class="inline-block absolute-para">定位absolute定位</p>
    </div>你在干什么</div>

    <br/>
    <div id="example3">例子3: 今天是个好日子<div class="para-wrapper">段落
        <p class="inline-block fixed-para">定位fixed 定位</p>
    </div>你在干什么</div>

    <br/><br/><br/>
    <div id="example4">例子4: 今天是个好日子<div class="para-wrapper">段落
        <span class="inline-block fixed-para">定位fixed span</span>
    </div>你在干什么</div>
    <br/><br/><br/>
    <div id="example5">例子5: 今天是个好日子<div class="para-wrapper">段落
        <span class="inline-block absolute-para">定位absolute span</span>
    </div>你在干什么</div>

    <br/><br/><br/>
    <div id="example6">例子6: 今天是个好日子<div class="para-wrapper">段落
        <span class="inline-block"> span</span>
    </div>你在干什么</div>

    <script>
       console.log(1, document.getElementById('example1').innerText)
       console.log(2, document.getElementById('example2').innerText)
       console.log(3, document.getElementById('example3').innerText)
       console.log(4, document.getElementById('example4').innerText)
       console.log(5, document.getElementById('example5').innerText)
       console.log(6, document.getElementById('example6').innerText)
    </script>

</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值