滚动后更改文本

Rot*_*075 5 html javascript jquery

您好我在用户滚动页面后有一个关于更改文本的问题.

h1当用户滚动时,如何将我的文本从YES 更改为NO?我已经尝试了几样.append(),.html()但没有成功.

我的代码:JSFIDDLE

HTML

<h1>YES</h1>

<article style="height: 1000px">
    <p>test</p>
</article>
Run Code Online (Sandbox Code Playgroud)

JS

$(document).ready(function () {

    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
            //change yes to no
        } else {
            //set h1 text to yes
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

我想要这个的原因是因为在使用的情况下.html(),我可以添加内联的html对象,如:.html('<span>yes</span>')

Vis*_*sad 8

试试这个

工作FIDLE

$(document).ready(function () {

    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) { 
            //change yes to no
            $('h1').html('No');
        } else {
            //set h1 text to yes
            $('h1').html('Yes');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 是的......两种情况都是正确的.. rit ?? (2认同)