Javascript JQuery if条件工作不正常

Javascript JQuery if条件工作不正常,javascript,jquery,css,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')

在我的网页中,我使用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();
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>
if条件始终作为true执行。请帮我解决这个问题。…

$(“#frmail”)
不能是falsy,即使没有具有该id的元素

替换

if ($('#frmail'))

$(“#frmail”)
不能是虚假的,即使没有具有该id的元素

替换

if ($('#frmail'))


请正确缩进代码。因为缩进是随机的,所以可能有我们看不到的bug。你不能维护这样的代码。我让每个人都来测试,但我不知道会发生什么不同。谢谢你的回答。如果你发现了问题,请告诉我问题是我不知道你期望的是什么。问题是工具提示没有按照我的代码显示。如果条件始终执行true blockPlease,请正确缩进代码。因为缩进是随机的,所以可能有我们看不到的bug。你不能维护这样的代码。我让每个人都来测试,但我不知道会发生什么不同。谢谢你的回答。如果你发现了问题,请告诉我问题是我不知道你期望的是什么。问题是工具提示没有按照我的代码显示。如果条件总是执行true blocking,您可以构建一个可测试的问题示例吗?现在你可能会发现问题所在,如果你发现问题所在,请告诉我你能为这个问题建立一个可测试的例子吗?在上面做就可以了。现在你可能会发现问题所在,如果你发现问题所在,请告诉我id为
frmail
的输入是否消失了?只要它在DOM中,此代码将计算为true。id为
frmail
的输入是否会消失?只要它在DOM中,此代码的计算结果将为true。
if ($('#frmail'))
if ($('#frmail').length)
if ($('#frmail').length > 0)