JQuery如果条件不能正常工作

时间:2013-01-11 20:37:12

标签: javascript jquery css

在我的网页中,我使用Jquery程序为指定字段的用户显示工具提示。 但我需要在某些其他位置显示某些字段的提示。为此我使用if条件来更改控件。但它不能正常工作我需要的方式。我没有得到它的问题。代码:

  $(window).load(function(){

 /*
 Display Tooltips on hovering over the input fields if an
 alt tag is present
  */
if($('#frmail'))
    {
  $('input').hover(function()
 {
   var thisItem = $(this);
  var msgTip = thisItem.attr('alt');

   if(msgTip.length)
   {

     $('body').append('<div id="menuTooltip">\
        <p>'+  msgTip +'</p>\</div>');

    var pos = thisItem.offset();  
    var width = thisItem.width();

问题代码:

    if($('#frmail'))
    {
    $("#menuTooltip").css( { "left": (pos.left + 95) + "px", "top":pos.top - 110 + "px" } );
    $("#menuTooltip").fadeIn('slow');
    }
    else
        {
        $("#menuTooltip").css( { "left": (pos.left + 115) + "px", "top":pos.top - 90 + "px" } );
        $("#menuTooltip").fadeIn('slow');
        }
      }

     }, function()
    {

     $('div#menuTooltip').remove();

    });
    }
else
    {}
});

html问题代码是

   <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>Welcome</title>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
      <script type='text/javascript' src='js/jquery-1.8.3.js'></script>
   <body>
  <div id="topnav" class="topnav">            
 <a href="#" class="fr"><span>Forgot Password</span></a>        
</div>  
 <div id="fr_menu">      
          <form id="fr">
                <label for="username">Username Or Email-ID</label>
                <input type="text" name="username"  id="frmail" alt="Please check your username is correct before clicking Resend." />
         <input type="submit" id="send" value=""/>
         </form>
            </div>
        <p></p>

        </div>


  </div> <!-- end .header -->

   <div class="container">
     <div class="note"></div>
   <div class="features"></div>
   <div class="reg">
  <h2>Didn't have an Account ?Create now.<center>It's Free !!</center></h2>

        <form action="login" method="post">
        <input type="text" name="fname" class="rtext" id="name" placeholder="First Nmae" alt="Enter your first name<br>Ex:<br> James P.J.<br>Enter (James)."/>

        <input type="button" value="" id="next"/>
        </form>
      </div>
     </body></html>

css代码:

  #topnav a.menu-open {
background:url("img/bg.png") repeat-x #222222;
color:#666!important;
outline:none;
  }

 #fr_menu {
-moz-border-radius:5px;
-webkit-border-radius:5px;
-webkit-box-shadow: 0px 3px 4px #fff;
 -moz-box-shadow: 0px 3px 4px ;
 box-shadow: 0px 3px 4px ; display:none;
background:url("img/bg.png") repeat-x #222222;
position:absolute;
width:210px;
z-index:100;
border:1px transparent;
text-align:left;
padding:10px;
margin-left:-8px;
*margin-left:-15px;
margin-top:35px;
color:#789;
font-size:11px;
   }

    #fr_menu input[type=text]{
display:block;
-moz-border-radius:10px;
-webkit-border-radius:10px;
border-radius:10px;
border:1px solid #ACE;
  -moz-box-shadow: 0px 1px 0px #777;
  -webkit-box-shadow: 0px 1px 0px #777;
  background: #ddd url('img/inputSprite.png') no-repeat 4px 5px;
  background: url('img/inputSprite.png') no-repeat 4px 5px, -moz-linear-gradient(
       center bottom,
       rgb(225,225,225) 0%,
       rgb(215,215,215) 54%,
       rgb(173,173,173) 100%
       );
  background:  url('img/inputSprite.png') no-repeat 4px 5px, -webkit-gradient(
      linear,
      left bottom,
      left top,
      color-stop(0, rgb(225,225,225)),
      color-stop(0.54, rgb(215,215,215)),
      color-stop(1, rgb(173,173,173))
      );
  color:#333;
  text-shadow:0px 1px 0px #FFF;
font-size:13px;
margin:10px 0 5px;
padding: 7px 14px 7px 30px;
width:80%;
 }
 #menuTooltip{
  width: 160px;
  min-height:90px;
  position: absolute; display: none;
 text-align:center;
 font-family:Arial;
 font-size:12px;
 background-image:url('../images/tooltip_ao.png');
 background-repeat:no-repeat;
}

 #menuTooltip p {
 width:145px;
 margin:5px;
 height:60px;
 margin-top:10px;
   }

if条件始终执行为true。请帮我解决这个问题....

2 个答案:

答案 0 :(得分:3)

即使没有带有该ID的元素,

$('#frmail')也不会是假的。

替换

if ($('#frmail'))

if ($('#frmail').length)

答案 1 :(得分:2)

if ($('#frmail').length > 0)