我试图根据一个变量来摇动我的登录表单,如果登录凭据正确,则该变量为空。这个变量是msg。
现在,如果您看到这个jsfiddle 示例(通过单击运行进行测试,它会摇晃),它工作正常,但如果我尝试在我的应用程序中使用,那么它会给出错误,如$this is not definedin line $this.css({。
我已经写$(this).css({在我的代码中,但控制台显示错误$this.css({。
请注意,它不必对变量做任何事情,因为我从服务器端获取变量,就像用户填写错误凭据时$message非空一样。我已经使用alert box.
我在我的应用程序中使用 jQuery 1.7。
我的应用程序中使用的代码是:
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;">
<script type="text/javascript" src="xyz/js/jquery.js"></script>  
<script type="text/javascript">
$(function() {
$("input:text:visible:first").focus();
var msg = "$message"
if (msg.length!==0){
alert("message is not empty");
jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
  this.each(function() {
    $(this).css({
      position: "relative"
    });
    for (var x = 1; x <= intShakes; x++) {
      $(this).animate({
        left: (intDistance * -1)
      }, (((intDuration / intShakes) / 4))).animate({
        left: intDistance
      }, ((intDuration / intShakes) / 2)).animate({
        left: 0
      }, (((intDuration / intShakes) / 4)));
    }
  });
  return this;
};
$('#login').shake(2, 13, 250); }   });</script>
</head>
<body class="login login-action-login wp-core-ui">
<div id="wrapper">
<div id="login">
    <form name="form_login1" action="$action" method="post">
        <input type="hidden" name="command" value="login">
    <p><label>Username<br>
    <if condition="enable_password_cookie">
        <input type="text" name="login2" value="$login_from_password_cookie" onchange="document.form_login2.login.value = this.value" id="user_login" class="input" size="20" tabindex="1">
    </if>
    <else>
        <input type="text" id="user_login" class="input" name="login2" size="20" onchange="document.form_login2.login.value = this.value">
    </else>
       </label></p>        
    </form>
    <form name="form_login2" action="$action" method="post">
        <input type="hidden" name="login">
        <input type="hidden" name="command" value="login">
        <input type="hidden" name="extra_param" value="$extra_param">
        <p>
                <label>Password
        <input type="password" name="password" is="password" id="password" class="input" value="" size="20">
        </label>
            </p>     
  <p class="submit">
    <input type="submit" value="Login" onclick="javascript:submit_login();" id="wp-submit" class="button button-primary button-large">
    </p><br><br>
     <p class="error">$message</p>  
</div><br>
    </form>
</div>       
</div>
</html>
PS:我已经尝试清理缓存,但我仍然收到此错误:http: //i.imgur.com/TNBgQM8.png
编辑 2:我已经清理了缓存和所有浏览器数据,但我仍然可以看到它正在更改$(this).css为$this.css. 我不明白为什么?:/