如何解决Jquery冲突?

时间:2014-07-11 04:37:17

标签: javascript

我创建了jquery代码,当一个值分配给输入时,这将显示一个div。

以下是demo working:

<script>  
 $(document).ready(function () {
   if ($("#mont1").val() == '9') {
     $("#di1").show();
   }
 });
</script> 

<input id="mont1" value="9" />
<div id="di1">
  <input type="submit" value="Submit" />
</div>

但是在我的项目中我收到了这个错误:

$(document).ready(function(){ Uncaught ReferenceError: $ is not defined

我有很多脚本:

<script src="/javascripts/prototype.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/effects.js?1375798766" type="text/javascript"></script> 
<script src="/javascripts/dragdrop.js?1375798766" type="text/javascript"></script> 
<script src="/javascripts/controls.js?1375798766" type="text/javascript"></script> 
<script src="/javascripts/application.js?1393972990" type="text/javascript"></script> 
<script src="/javascripts/jquery/jquery-1.4.min.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/jquery/easyTooltip.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/jquery/jquery-ui-1.7.2.custom.min.js?1375798764"   type="text/javascript"></script><script src="/javascripts/jquery/jquery.wysiwyg.js?1375798764" type="text/javascript"></script>
<script src="/javascripts/jquery/hoverIntent.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/jquery/superfish.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/jquery/jquery_popup.js?1405027324" type="text/javascript"></script>  
<script src="/javascripts/jquery/jquery.validationEngine.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/jquery/jquery.validationEngine-en.js?1403629546" type="text/javascript"></script><script src="/javascripts/calendar_date_select/calendar_date_select.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/calendar_date_select/format_hyphen_ampm.js?1375798764" type="text/javascript"></script> 
<script src="/javascripts/autoNumeric.js?1375798766" type="text/javascript"></script> 

有人可以帮我解决这个jquery冲突吗?

2 个答案:

答案 0 :(得分:2)

问题在于jsfiddle链接发布了什么:

您的脚本包含在onLoad事件中。所以这条线无法执行。

window.onload=function(){ 

要么将包裹更改为No wrap in head,它就能正常工作。或者你可以简单地删除jsfiddle中的匿名函数:

window.onload=function(){ //<-- remove this
   ...
} //<-- remove this

修改

  

脚本

jQuery(document).ready(function ($) {

    $(document).ready(function () {
        if ($("#mont1").val() == '9') {
            $("#di1").show();
        }
    });

});

答案 1 :(得分:0)

你可以添加一个合适风格的课程

.hidden { display: block; }
.inline { display: inline; }

在js

window.onload=function(){ 
  var inputval = document.getElementById("input-hi").value;
  var div = document.getElementById("div-hi");
  if (inputval == 5)
     div.className = "hidden";
  else
     div.className = 'inline';
}