尝试创建 CSS 下拉菜单时未定义接收“$”

Posted

技术标签:

【中文标题】尝试创建 CSS 下拉菜单时未定义接收“$”【英文标题】:Receiving '$' is undefined when trying to create a CSS drop down menu 【发布时间】:2014-04-24 13:11:44 【问题描述】:

我正在尝试使用一些 CSS 和 javascripthtml 页面上创建一个下拉菜单。 CSS 似乎工作正常,但 JS 返回'$' is undefined。我不确定我在这里做错了什么。当单击 CRM 2011 功能区中的按钮时,我已设置为打开网页。目前它设置为仅在本地运行。

HTML:

<html>
<header>
<link rel="stylesheet" type="text/css" href="OpportunityStyleSheet.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="createRecord.js"></script>  
</header>

<body>
    <h3>Select your Products</h3>

    <div id='cssmenu'>
<ul>
   <li class='active'><a href='index.html'><span>Home</span></a></li>
   <li class='has-sub'><a href='#'><span>Products</span></a>
      <ul>
         <li class='has-sub'><a href='#'><span>Product 1</span></a>
            <ul>
               <li><a href='#'><span>Sub Item</span></a></li>
               <li class='last'><a href='#'><span>Sub Item</span></a></li>
            </ul>
         </li>
         <li class='has-sub'><a href='#'><span>Product 2</span></a>
            <ul>
               <li><a href='#'><span>Sub Item</span></a></li>
               <li class='last'><a href='#'><span>Sub Item</span></a></li>
            </ul>
         </li>
      </ul>
   </li>
   <li><a href='#'><span>About</span></a></li>
   <li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</div>
</body>
</html>

JS:

$('#cssmenu li.has-sub>a').on('click', function()
        $(this).removeAttr('href');
        var element = $(this).parent('li');
        if (element.hasClass('open')) 
            element.removeClass('open');
            element.find('li').removeClass('open');
            element.find('ul').slideUp();
        
        else 
            element.addClass('open');
            element.children('ul').slideDown();
            element.siblings('li').children('ul').slideUp();
            element.siblings('li').removeClass('open');
            element.siblings('li').find('li').removeClass('open');
            element.siblings('li').find('ul').slideUp();
        
    );

function createRecord() 
alert("Button Pressed");

createRecord() 函数确实有效并显示正确的警报。感谢您的帮助!

错误消息。 Microsoft Dynamics CRM 错误报告内容

<CrmScriptErrorReport>
  <ReportVersion>1.0</ReportVersion>
  <ScriptErrorDetails>
   <Message>'$' is undefined</Message>
   <Line>6</Line>
   <URL>/_common/global.ashx?ver=66087337</URL>
   <PageURL>/userdefined/edit.aspx?etc=3&id=%7b515C22D0-7524-E311-8980-12A32B1E376A%7d&pagemode=iframe&preloadcache=1395165209284</PageURL>
   <Function>anonymous(container,scriptContent,id)if(IsNull(container))container=this.get_headElement();var$v_0=container.ownerDocument.createElement("script");container.appendChild($v_0);!isNullOrEmptyString(id)&&$v_0.setAttribute("id",id);$v_0.setAttribute("type","</Function>
   <CallStack>
    <Function>anonymous(container,scriptContent,id)if(IsNull(container))container=this.get_headElement();var$v_0=container.ownerDocument.createElement("script");container.appendChild($v_0);!isNullOrEmptyString(id)&&$v_0.setAttribute("id",id);$v_0.setAttribute("type","text/javascript");$v_0.text=scriptContent</Function>
    <Function>anonymous(container,scriptFile)var$v_0=scriptFile.toString();if(this.$4W_1($v_0))return;var$v_1=this.fetchExternalFile($v_0);this.addIncludeInline(container,$v_1,$v_0)</Function>
    <Function>anonymous(uri,useInlineScripts,scriptLoaded)if(uri.get_path().toUpperCase()==="/_STATIC/_COMMON/SCRIPTS/GLOBAL.JS")uri=Mscrm.CrmUri.create("/_common/global.ashx");if(useInlineScripts)Mscrm.CrmHeader.get_scriptLoader().addIncludeExternalSync(null,uri);elseMscrm.CrmHeader.get_scriptLoader().addIncludeExternalCallback(null,uri,scriptLoaded)</Function>
    <Function>loadWebResourceScript(sScriptSrc,sId)</Function>
    <Function>LoadJsScripts()</Function>
    <Function>executeAction(action)</Function>
    <Function>anonymous()executeAction(action);executeActionQueue()</Function>
   </CallStack>
  </ScriptErrorDetails>
  <ClientInformation>
   <BrowserUserAgent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BRI/2; BOIE9;ENUS)</BrowserUserAgent>
   <BrowserLanguage>en-us</BrowserLanguage>
   <SystemLanguage>en-us</SystemLanguage>
   <UserLanguage>en-us</UserLanguage>
   <ScreenResolution>1600x900</ScreenResolution>
   <ClientName>Outlook Laptop - Online</ClientName>
   <ClientTime>2014-03-18T11:53:30</ClientTime>
  </ClientInformation>
  <ServerInformation>
    <OrgLanguage>1033</OrgLanguage>
    <OrgCulture>1033</OrgCulture>
    <UserLanguage>1033</UserLanguage>
    <UserCulture>1033</UserCulture>
    <OrgID>AACB118A-823E-4A12-BFCD-190E5E9BE731</OrgID>
    <UserID>E200F73B-57CC-E111-B35F-12A32B1E376A</UserID>
    <CRMVersion>5.0.9690.3731</CRMVersion>
  </ServerInformation>
</CrmScriptErrorReport>

<CrmScriptErrorReport>
  <ReportVersion>1.0</ReportVersion>
  <ScriptErrorDetails>
   <Message>'$' is undefined</Message>
   <Line>6</Line>
   <URL>/_common/global.ashx?ver=66087337</URL>
   <PageURL>/userdefined/edit.aspx?etc=3&id=%7b515C22D0-7524-E311-8980-12A32B1E376A%7d&pagemode=iframe&preloadcache=1395165209284</PageURL>
   <Function>anonymous(container,scriptContent,id)if(IsNull(container))container=this.get_headElement();var$v_0=container.ownerDocument.createElement("script");container.appendChild($v_0);!isNullOrEmptyString(id)&&$v_0.setAttribute("id",id);$v_0.setAttribute("type","</Function>
   <CallStack>
    <Function>anonymous(container,scriptContent,id)if(IsNull(container))container=this.get_headElement();var$v_0=container.ownerDocument.createElement("script");container.appendChild($v_0);!isNullOrEmptyString(id)&&$v_0.setAttribute("id",id);$v_0.setAttribute("type","text/javascript");$v_0.text=scriptContent</Function>
    <Function>anonymous(container,scriptFile)var$v_0=scriptFile.toString();if(this.$4W_1($v_0))return;var$v_1=this.fetchExternalFile($v_0);this.addIncludeInline(container,$v_1,$v_0)</Function>
    <Function>anonymous(uri,useInlineScripts,scriptLoaded)if(uri.get_path().toUpperCase()==="/_STATIC/_COMMON/SCRIPTS/GLOBAL.JS")uri=Mscrm.CrmUri.create("/_common/global.ashx");if(useInlineScripts)Mscrm.CrmHeader.get_scriptLoader().addIncludeExternalSync(null,uri);elseMscrm.CrmHeader.get_scriptLoader().addIncludeExternalCallback(null,uri,scriptLoaded)</Function>
    <Function>anonymous($p0,$p1,$p2,$p3)Mscrm.CrmHeader.setScriptFile(Mscrm.CrmUri.create($p1),true);for(var$v_0=window,$v_1=$p0.split("."),$v_2=0;$v_2<$v_1.length;$v_2++)if($v_0)$v_0=$v_0[$v_1[$v_2]];if(!IsNull($v_0)&&typeof$v_0===Mscrm.TypeNames.functionType)var$v_3=this.$CN_1($p2,$p3);return$v_0.apply(null,$v_3)returnnull</Function>
    <Function>anonymous($p0,$p1,$p2,$p3)var$v_0=this.getCommandDefinition($p0,$p1);if(IsNull($v_0)||IsNull($v_0.Actions))returnfalse;for(var$v_1=0;$v_1<$v_0.Actions.length;$v_1++)var$v_2=$v_0.Actions[$v_1];switch($v_2.ActionType)case2:this.$DF_1($v_2.Attributes);break;case3:var$v_3=$v_2.Attributes;this.$9z_1($v_3.FunctionName,$v_3.Library,$v_2.Parameters,$p2);break;case1:this.$DG_1($v_2.Attributes,$v_2.Parameters);breakreturntrue</Function>
    <Function>anonymous($p0,$p1,$p2)var$v_0=this.parseCommandFromRibbon($p0),$v_1=this.$7w_1($v_0.command,$v_0.entityLogicalName,$p1,$p2),$v_2=$p1;if(!IsNull($v_2)&&!isNullOrEmptyString($v_2.PopulationXML)&&!$v_2.SuppressCommandIncludes)var$v_3=null;if($v_0.entityContext==="Form")var$v_4=$find("crmFormSelector");if($v_4)$v_3=$v_4.get_currentFormId()$v_2.PopulationXML=this.$Ep_1($v_2.PopulationXML,$v_0.entityLogicalName,$v_3)return$v_1</Function>
    <Function>anonymous($p0,$p1,$p2)if(this.$1w_1&&!this.$1w_1.$3m_1)returnthis.$1w_1.handleCommand($p0,$p1,$p2);returnfalse</Function>
    <Function>anonymous(handler,commandId,properties,sequenceNumber)returnhandler.handleCommand(commandId,properties,sequenceNumber)</Function>
    <Function>anonymous($p0,$p1,$p2)var$v_0=this.$2S_0[$p0];if(CUI.ScriptUtility.isNullOrUndefined($v_0))returnfalse;elseif(Array.isInstanceOfType($v_0))for(var$v_1=$v_0,$v_2=false,$v_3=0;$v_3<$v_1.length;$v_3++)var$v_4=$v_1[$v_3];if(this.callCommandHandler($v_4,$p0,$p1,$p2))$v_2=truereturn$v_2elsereturnthis.callCommandHandler($v_0,$p0,$p1,$p2)</Function>
    <Function>anonymous(commandId,properties)returnthis.$DE_0(commandId,properties,this.getNextSequenceNumber())</Function>
    <Function>anonymous(commandId,properties,commandInfo,root)returnthis.$10_1.executeCommand(commandId,properties)</Function>
    <Function>anonymous($p0)if(CUI.ScriptUtility.isNullOrUndefined($p0.$4_1))return;$p0.$7U_1=this.$DU_1();var$v_0=$p0.get_commandInfo();$v_0.CommandId=$p0.$4_1;$v_0.RootId=this.$4_0;$v_0.RootType=this.get_rootType();!CUI.ScriptUtility.isNullOrUndefined(this.$26_1)&&this.$26_1.executeRootCommand($p0.$4_1,$p0.$34_1,$v_0,this)</Function>
    <Function>anonymous($p0)this.$7w_1($p0);returntrue</Function>
    <Function>anonymous($p0)if($p0.$I_1!==11)$p0.get_commandInfo().RootLocation=$p0.$I_1===2?"UpperRibbon":"LowerRibbon";if($p0.$I_1===2)var$v_0=$p0.$34_1;$p0.get_commandInfo().TabId=$v_0.NewContextIdreturnCUI.Root.prototype.$2p_0.call(this,$p0)</Function>
    <Function>anonymous($p0)this.$2p_0($p0)&&!CUI.ScriptUtility.isNullOrUndefined(this.$M_0)&&this.$M_0.$B0_0($p0);this.$Ao_0($p0)</Function>
    <Function>anonymous($p0)this.$2p_0($p0)&&!CUI.ScriptUtility.isNullOrUndefined(this.$M_0)&&this.$M_0.$B0_0($p0);this.$Ao_0($p0)</Function>
    <Function>anonymous($p0)this.$2p_0($p0)&&!CUI.ScriptUtility.isNullOrUndefined(this.$M_0)&&this.$M_0.$B0_0($p0);this.$Ao_0($p0)</Function>
    <Function>anonymous($p0)this.$2p_0($p0)&&!CUI.ScriptUtility.isNullOrUndefined(this.$M_0)&&this.$M_0.$B0_0($p0);this.$Ao_0($p0)</Function>
    <Function>anonymous($p0)this.$2p_0($p0)&&!CUI.ScriptUtility.isNullOrUndefined(this.$M_0)&&this.$M_0.$B0_0($p0);this.$Ao_0($p0)</Function>
    <Function>anonymous($p0)this.$2p_0($p0)&&!CUI.ScriptUtility.isNullOrUndefined(this.$M_0)&&this.$M_0.$B0_0($p0);this.$Ao_0($p0)</Function>
   </CallStack>
  </ScriptErrorDetails>
  <ClientInformation>
   <BrowserUserAgent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BRI/2; BOIE9;ENUS)</BrowserUserAgent>
   <BrowserLanguage>en-us</BrowserLanguage>
   <SystemLanguage>en-us</SystemLanguage>
   <UserLanguage>en-us</UserLanguage>
   <ScreenResolution>1600x900</ScreenResolution>
   <ClientName>Outlook Laptop - Online</ClientName>
   <ClientTime>2014-03-18T11:53:33</ClientTime>
  </ClientInformation>
  <ServerInformation>
    <OrgLanguage>1033</OrgLanguage>
    <OrgCulture>1033</OrgCulture>
    <UserLanguage>1033</UserLanguage>
    <UserCulture>1033</UserCulture>
    <OrgID>AACB118A-823E-4A12-BFCD-190E5E9BE731</OrgID>
    <UserID>E200F73B-57CC-E111-B35F-12A32B1E376A</UserID>
    <CRMVersion>5.0.9690.3731</CRMVersion>
  </ServerInformation>
</CrmScriptErrorReport>

【问题讨论】:

需要 JSfiddle。你在加载 Jquery 吗? 你用谷歌搜索你的问题吗? google.com/search?q=%27%24%27+is+undefined 你是如何运行页面的?来自网络服务器还是来自file://? - createRecord 也可以正常工作,因为它是原生的 Javascript。 将脚本 src 从 //ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js 更改为 ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js 是的,我在发布之前先用 Google 搜索了我的问题。我已经尝试过所有提到的 jscript 库符号,它们都返回相同的问题。这是更详细的错误消息。由于无法放入评论,因此我将其发布在原始帖子中。 【参考方案1】:

我认为您只需要在本地运行时附加 http:,稍后可以将其删除,因为“//”将作为大约协议的前缀。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

PS:在每个文档中都提到 head、meta、title 标签总是好的。与上述问题无关。这是干净和正确的写作方式。 :)

您的 html 文档...按要求:

<html>
<head>
<link rel="stylesheet" type="text/css" href="OpportunityStyleSheet.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="createRecord.js"></script>  

</head>

<body>
<h3>Select your Products</h3>

<div id='cssmenu'>
<ul>
<li class='active'><a href='index.html'><span>Home</span></a></li>
<li class='has-sub'><a href='#'><span>Products</span></a>
  <ul>
     <li class='has-sub'><a href='#'><span>Product 1</span></a>
        <ul>
           <li><a href='#'><span>Sub Item</span></a></li>
           <li class='last'><a href='#'><span>Sub Item</span></a></li>
        </ul>
     </li>
     <li class='has-sub'><a href='#'><span>Product 2</span></a>
        <ul>
           <li><a href='#'><span>Sub Item</span></a></li>
           <li class='last'><a href='#'><span>Sub Item</span></a></li>
        </ul>
     </li>
  </ul>
</li>
<li><a href='#'><span>About</span></a></li>
<li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</div>
<script type="text/javascript">
$('#cssmenu li.has-sub>a').on('click', function()
    $(this).removeAttr('href');
    var element = $(this).parent('li');
    if (element.hasClass('open')) 
        element.removeClass('open');
        element.find('li').removeClass('open');
        element.find('ul').slideUp();
    
    else 
        element.addClass('open');
        element.children('ul').slideDown();
        element.siblings('li').children('ul').slideUp();
        element.siblings('li').removeClass('open');
        element.siblings('li').find('li').removeClass('open');
        element.siblings('li').find('ul').slideUp();
    
);
function createRecord() 
alert("Button Pressed");

</script>
</body>
</html>

干杯,

阿肖克

【讨论】:

您好,感谢您的回复。我会记住标签评论。我尝试过以多种不同方式引用 jscript 库。我每次都收到相同的错误。我在原始帖子中发布了完整的消息。谢谢你的帮助。我更改了“crmserver”引用的所有 css 和 jscript 引用。其中之一是一个 jquery,它可以在我在 CRM 中运行的其他 Web 资源上运行。【参考方案2】:

您似乎没有加载正确的 JQuery 库。请向我们提供完整的头部部分。

看看这个主题:JavaScript runtime error: '$' is undefined 类似的问题。

添加: 我现在可以看到 head 部分 - “//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js”不是正确的库 url - 添加 http: 到它。

【讨论】:

// 表示协议relative/agnostic,因此可以在httphttps 上工作,但是当您直接从file:// 协议运行它时将无法工作。 修复会解决问题,但解释不正确。 "//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" 是正确的 url,假设您从网络服务器运行代码(您应该始终这样做) 您好,感谢您的回复。在问这个问题之前,我确实看到了你发布链接的那个问题。它返回相同的错误消息。【参考方案3】:

我能够解决这个问题。 jscript库基本上需要调用两次。一次在 html 文件中(就像您在上面建议的那样),然后再次在函数运行的 CRM 实体上。我不认为这需要发生,因为在单击调用该函数以显示网页的按钮之前,什么都没有被调用。然而,显然我错了。

感谢大家的帮助和cmets。

【讨论】:

以上是关于尝试创建 CSS 下拉菜单时未定义接收“$”的主要内容,如果未能解决你的问题,请参考以下文章

Foreach 并继续创建子菜单列

asp二级联动下拉菜

IE & Firefox - 自定义下拉菜单无法移除原生箭头

带动画的 CSS 下拉菜单(无 js)

Jquery下拉菜单单击始终打开

使用 CSS 和 PHP/MySQL 的动态下拉菜单