为什么 jQuery fadeIn() 和 fadeOut() 在这个例子中看起来很奇怪?

发布于 2024-08-28 17:46:03 字数 2957 浏览 3 评论 0原文

我一直在 ASP.NET 项目中使用 jQuery,发现 .fadeIn()fadeOut() 函数有一些奇怪的行为。在下面的示例中,单击按钮 (ID Button1) 应该会导致 ID 为 Label1 的文本范围和 ID 为 的按钮TextBox1 执行以下操作:

  • 淡出 将
  • 文本框的文本和文本范围更改为 您单击了按钮
  • 淡入

基于我正在使用的浏览器,我得到了 3 种不同的场景,每个元素在每种情况下的功能都不同。以下是我实际单击按钮时发生的情况:

TextBox1

  • 在 IE8 中,文本框淡出,更改文本,然后淡入
  • 在 IE8 兼容性视图中,文本框淡出,更改文本,然后淡入。但是,框中的文本看起来与单击按钮之前略有不同。
  • 在 FireFox 3.5.8 中,文本框不会淡出(但它会“暂停”淡出所需的时间),会更改文本,然后似乎会在淡入的地方再次“暂停”

Label1:

  • 在 IE8 中,标签不会淡出(但它会“暂停”淡出所需的时间),会更改文本,然后似乎再次“暂停”。 。
  • 在 IE8 兼容性视图中,标签淡出、更改文本,然后淡入,但文本看起来与单击按钮之前略有不同
  • 在 FireFox 3.5.8 中,标签不会淡出(但它会“暂停”淡出所需的时间),会更改文本,然后似乎会在淡入的地方再次“暂停”。

有两个问题:

  1. 如何使每个元素在不同的浏览器中表现不同?
  2. 有没有更好的方法来跨多个平台获得我正在寻找的功能?

这是该文件的源代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>

</title>
    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#Button1").click(function(event) {
          $("#Label1").fadeOut("slow", function() {
            $(this).text("You clicked the button");            
            $(this).fadeIn("slow");
          });
          $("#TextBox1").fadeOut("slow", function() {
            $(this).val("You clicked the button").fadeIn("slow");            
            $(this).fadeIn("slow");
          });
          event.preventDefault();
        });
        $("a").click(function(event) {
          $("#Label1").text("You clicked the link");
          $("#TextBox1").val("You clicked the link");
          event.preventDefault();
        });
      });

    </script>

</head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTQwMjM5ODcyZGT6OfedWuFhLrSUyp+gwkCEueddvg==" />
</div>

<div>

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK56uWtBwLs0bLrBgKM54rGBotkyyA5RRsPBGNaPTPCe7F5ARwv" />
</div>
    <div>

      <span id="Label1" style="color:#009900;">Type Something Here:</span>
&nbsp;
      <a href="http://www.google.com">This is a test Link</a>  
      <input name="TextBox1" type="text" value="test" id="TextBox1" style="width:258px;" />
      <br />
      <br />
      <input type="submit" name="Button1" value="Button" id="Button1" />

    </div>
    </form>
</body>
</html>

I've been playing with jQuery in an ASP.NET project and am finding some odd behavior with the .fadeIn() and fadeOut() functions. In the below example, a click on the button (ID Button1) is supposed to cause both the span of text with ID Label1 and the the button with the ID TextBox1 to do the following things:

  • Fade Out
  • Change the text of both the text box and the span of text to be You clicked the button
  • Fade In

Based on the browser I'm using, I get 3 different scenarios, and each element functions differently in each situation. Here's what happens when I actually click the button:

TextBox1:

  • In IE8, the text box fades out, changes text, then fades back in
  • In IE8 Compatibility View, the text box fades out, changes text, then fades back in. However, the text in the box looks a little different than before the button was clicked.
  • In FireFox 3.5.8, the text box doesn't fade out (but it does "pause" for the amount of time the fade would take), does change the text, then seems to "pause" again where it would be fading in.

Label1:

  • In IE8, the label doesn't fade out (but it does "pause" for the amount of time the fade would take), does change the text, then seems to "pause" again where it would be fading in.
  • In IE8 Compatibility View, the label does fade out, change text, and fades back in, but the text looks a little different than before the button was clicked.
  • In FireFox 3.5.8, the label doesn't fade out (but it does "pause" for the amount of time the fade would take), does change the text, then seems to "pause" again where it would be fading in.

Two questions:

  1. What's going in to make each element to behave differently in different browsers?
  2. Is there a better way to get the functionality I'm looking for across multiple platforms?

Here's the source code of the file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>

</title>
    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#Button1").click(function(event) {
          $("#Label1").fadeOut("slow", function() {
            $(this).text("You clicked the button");            
            $(this).fadeIn("slow");
          });
          $("#TextBox1").fadeOut("slow", function() {
            $(this).val("You clicked the button").fadeIn("slow");            
            $(this).fadeIn("slow");
          });
          event.preventDefault();
        });
        $("a").click(function(event) {
          $("#Label1").text("You clicked the link");
          $("#TextBox1").val("You clicked the link");
          event.preventDefault();
        });
      });

    </script>

</head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTQwMjM5ODcyZGT6OfedWuFhLrSUyp+gwkCEueddvg==" />
</div>

<div>

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwK56uWtBwLs0bLrBgKM54rGBotkyyA5RRsPBGNaPTPCe7F5ARwv" />
</div>
    <div>

      <span id="Label1" style="color:#009900;">Type Something Here:</span>
 
      <a href="http://www.google.com">This is a test Link</a>  
      <input name="TextBox1" type="text" value="test" id="TextBox1" style="width:258px;" />
      <br />
      <br />
      <input type="submit" name="Button1" value="Button" id="Button1" />

    </div>
    </form>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-09-04 17:46:03

我遇到了同样的问题。为了修复它,我尝试了这个并且它有效。

代替:

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

我使用:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

如果您确实需要 Visual Studio 版本,请尝试使用不同的版本或从不同的来源获取它。

I experienced the same problem. In order to fix it, I tried this and it worked.

Instead of:

<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>

I used:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

Try using a different version or getting it from a different source if you really need the Visual Studio version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文