【问题标题】:jquery change text and replace it with a linkjquery更改文本并将其替换为链接
【发布时间】:2013-01-15 08:33:21
【问题描述】:

当你用鼠标移动它时,我想更改一些文本 bij 用 url 替换它。 我知道您无法通过使用动画(用于 css)来更改它,这就是我使用淡出的原因。 花了几个小时后,我仍然无法弄清楚为什么它不起作用。

我还想要页面中心的整个 div,我尝试了 valign 等,但这也不起作用。这不是优先事项,但最好替换 margin-top

<div align="center" style="margin-top: 20%;"> 
  <div id="change">
    <p id="big">Welcome!</p>
  </div>
  <img src="pic.JPG" alt="Logo" width="40%" height="90"/>
</div>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$("#change").hover(
function(){
    $(this).find('p').fadeOut('slow', function() {
          $(this).text('<a href="Index.html">Continue</a>').fadeIn('slow'); 
    });
}, 
function(){
    $(this).find('p').fadeOut('slow', function() {
          $(this).text('Welcome').fadeIn('slow'); 
    });    
});
</script>

提前谢谢!

【问题讨论】:

    标签: jquery html hover center


    【解决方案1】:

    使用html方法:

    $(this).html('<a href="Index.html">Continue</a>').fadeIn('slow'); 
    

    【讨论】:

    • 似乎不是真正的问题。
    【解决方案2】:

    嗯,没有我想的那么容易。

    在快速移动鼠标时,mouseenter/over mouseleave/out 太容易触发,所以:

    <div align="center" style="margin-top: 20%;"> 
      <div id="change">
        <p id="big">Welcome!</p>
        <p id="link" style="display:none"><a href="Index.html">Continue</a></p>
      </div>
      <img src="pic.JPG" alt="Logo" width="40%" height="90"/>
    </div>
    

    使用

    $(function() {
      $("#change").hover(function(){
        $("#big").fadeOut('slow',function() {
          $("#link").fadeIn('slow');
        });
      },
      function(){
        $("#link").fadeOut('slow',function() {
          $("#big").fadeIn('slow');
        });
      });
    });
    

    几乎有效。但由于进入/离开事件,您可能会在屏幕上看到两者

    如果我们使用hoverIntent,它可以工作 - 您需要先下载并包含jquery.hoverIntent.minified.js

    DEMO

    $("#change").hoverIntent(function(){
        $("#big").fadeOut('slow',function() {
          $("#link").fadeIn('slow');
        });
      },
      function(){
        $("#link").fadeOut('slow',function() {
          $("#big").fadeIn('slow');
        });
      }    
    );
    

    或者如果你坚持要更改 html:DEMO

    $("#change").hoverIntent(function(){
        $("#big").fadeOut('slow',function() {
          $("#big").html('<a href="Index.html">Continue</a>').fadeIn('slow');
        });
      },
      function(){
        $("#big").fadeOut('slow',function() {
          $("#big").html('Welcome').fadeIn('slow');
        });
      }    
    );
    

    【讨论】:

    • 这行得通,但我想做的是替换文本。当您移开它时,它会继续出现,当您移出它时,欢迎再次出现
    • 无需更换即可。悬停同样的问题,我认为我们需要 hoverintent 插件
    • 这也不起作用。以前的方法更改了它,但是这两个根本没有显示任何更改:s 也许我应该将我所有的代码添加到另一个 div 中。但它只是一个容器......我不明白了
    • 如果您不发布完整的故事,当然无法帮助您。我的代码在您发布时使用您的 html 在小提琴中工作。您需要在 jQuery 之后包含 cherne.net/brian/resources/jquery.hoverIntent.minified.js
    • 可以通过电子邮件发送给您吗?我不喜欢发布我的整个页面
    猜你喜欢
    • 2011-04-28
    • 2011-09-19
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多