Javascript 动画HTML文本更改jQuery

Javascript 动画HTML文本更改jQuery,javascript,jquery,html,Javascript,Jquery,Html,我试图使用javascript和jquery更改HTML元素的文本。到目前为止,这是我的代码,我似乎无法让它工作。我在谷歌上搜索过,似乎找不到任何东西 $("div#title").hover( function() { $(this).stop().animate({ $("div#title").html("Hello") }, "fast"); }, function(){ $(this).st

我试图使用javascript和jquery更改HTML元素的文本。到目前为止,这是我的代码,我似乎无法让它工作。我在谷歌上搜索过,似乎找不到任何东西

$("div#title").hover(
    function() {
        $(this).stop().animate({
            $("div#title").html("Hello")
        }, "fast");
    },
    function(){
        $(this).stop.animate({
            $("div#title").html("Good Bye")
        }, "fast");
    }
);

非常感谢您提供的任何帮助。

您目前使用的语法不正确,您也无法将文本设置为动画,而是需要将包含文本的元素设置为动画。还不清楚您想要为哪些人物设置动画,这里有两个示例:

设置不透明度动画:

$("div#title").hover(

function () {
    $(this).stop().css('opacity', '0').html(function (_, oldText) { // Set the opacity of the div to 0 and then change the html (flip it based on last value)
        return oldText == 'Good Bye' ? 'Hello' : 'Good Bye'
    }).animate({
        opacity: 1 // Animate opacity to 1 with a duration of 2 sec
    }, 2000);
});
动画宽度:

$("div#title").hover(

function () {
    $(this).stop().animate({
        'width': '0px'  // Animate the width to 0px from current width
    }, 2000, function () {  // On completion change the text
        $(this).html(function (_, oldText) {
            return oldText == 'Good Bye' ? 'Hello' : 'Good Bye'
        }).animate({          // and animate back to 300px width.
            'width': '300px'
        }, 2000);
    })
});

文本不能真正鼓励,可以使用html方法更改内容。 Lettering.js是一个插件,可以以更直观的方式播放文本。

你也可以试试这个

$("div#title").hover(
   function() {
       $(this).stop().fadeOut("slow",function(){ 
                $(this).html("Hello")
            }).fadeIn("slow");
},
   function(){
       $(this).stop().fadeOut("slow", function(){
                $(this).html("Good Bye")
            }).fadeIn("slow");
});
打字风格

<font size="7" style="position:absolute; left:0px; top:0px; width:150px; z-index:50;">Hello World!</font>
    <div id="test" style="position:absolute; left:0px; top:0px; height:100px; width:150px; z-index:60; background-color: #ffffff;"></div>

$'test'。动画{left:150},2000年

我已经为此编写了一个插件,您可以在github上查看它:

您可以这样使用它:

bubbleText({
    element: $element,      // must be one DOM leaf node
    newText: 'new Text',    // must be one string
});
看看这个例子

希望您喜欢它:

选择一个HTML元素。 下面是我的例子:


<h1 id="heading">Heading1</h1>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<script>
  //Changing the text part
  $("#heading").text("1Heading");
  // No needed code
  var read = document.getElementById("heading");
  var text = read.value;
  console.log(text);
  //Should be 1Heading
</script>

你想在这里制作什么动画?嘿@Dangerousgame,这与问题无关,从我所能看出,它没有达到你的预期。
bubbleText({
    element: $element,      // must be one DOM leaf node
    newText: 'new Text',    // must be one string
});

<h1 id="heading">Heading1</h1>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<script>
  //Changing the text part
  $("#heading").text("1Heading");
  // No needed code
  var read = document.getElementById("heading");
  var text = read.value;
  console.log(text);
  //Should be 1Heading
</script>