代码在结果中工作但在博客中不工作

Code working in result but not working in blogger

我正在使用 jquery chosen Actually i want to implement the result like in the below image as it is working well in the result which is shown in the snippet. But when i have uploaded the same code in my blog,在 Blogger 中是 运行,它不起作用。我做对了一切。下面是我在下面的结果中实现的图像,它在堆栈片段结果中工作,但在我的博客中没有。

$(".chosen-select").chosen();
$(".chosen-select").bind('chosen:hiding_dropdown', function(e, i) {
  searched_value = i.chosen.get_search_text();
  firstElementOnDropdown = i.chosen.search_results.find('li.active-result').first()
  if (firstElementOnDropdown.text().toLowerCase() == searched_value.toLowerCase()) {
    firstElementOnDropdown.trigger('mouseup');
    var t = i;
    setTimeout(function() {
      t.chosen.input_blur();
    }, 150);
  }
});
<link rel="stylesheet" href="http://harvesthq.github.io/chosen/chosen.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script>
<div>
  <em>Multiple Select with Groups</em><br>
  <select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6">
    <option value=""></option>
    <optgroup label="NFC EAST">
      <option>Dallas Cowboys</option>
      <option>New York Giants</option>
      <option>Philadelphia Eagles</option>
      <option>Washington Redskins</option>
    </optgroup>
   
 
    
  </select>
</div>

我在我的博客中实现了下面的代码

<link rel="stylesheet" href="http://harvesthq.github.io/chosen/chosen.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script>
<div>
  <em>Multiple Select with Groups</em><br />
  <select data-placeholder="Your Favorite Football Team" style="width:350px;" class="chosen-select" multiple tabindex="6">
    <option value="" />
    <optgroup label="NFC EAST">
      <option />Dallas Cowboys
      <option />New York Giants
      <option />Philadelphia Eagles
      <option />Washington Redskins
    </optgroup>



  </select>
</div>


<script type="text/javascript">
$(".chosen-select").chosen();
$(".chosen-select").bind('chosen:hiding_dropdown', function(e, i) {
  searched_value = i.chosen.get_search_text();
  firstElementOnDropdown = i.chosen.search_results.find('li.active-result').first()
  if (firstElementOnDropdown.text().toLowerCase() == searched_value.toLowerCase()) {
    firstElementOnDropdown.trigger('mouseup');
    var t = i;
    setTimeout(function() {
      t.chosen.input_blur();
    }, 150);
  }
});
</script>

我发现当我打开我的博客时,我在控制台中收到 2 个错误。下面是

的图像

尝试隔离代码:

<script type="text/javascript">
//<![CDATA[

Your code here

//]]>
</script>

首先: 确保 jquery.min.js 文件必须在放置包含 jquery function 或 [= 的任何其他 .js 文件之前15=]。

这是 console

中显示错误的主要原因

其次:如果您使用的是jquery,您必须按以下方式编写代码:

$(function(){
    //your jquery code
 });

或者您可以使用

$(document).ready(function(){
    //your jquery code must be here
});

经过一些调试,我看到你的 if 语句:

if (firstElementOnDropdown.text().toLowerCase() == searched_value.toLowerCase())

返回错误。快速修复是:

if (firstElementOnDropdown.text().toLowerCase().trim() == searched_value.toLowerCase().trim())

因为有空格导致if子句出错。 如果不需要,使用 trim 函数删除字符串中的前导和尾随空格是一种很好的技术。

<script type="text/javascript">
$(".chosen-select").chosen();
$(".chosen-select").bind('chosen:hiding_dropdown', function(e, i) {
  searched_value = i.chosen.get_search_text();
  firstElementOnDropdown = i.chosen.search_results.find('li.active-result').first()
  if (firstElementOnDropdown.text().toLowerCase().trim() == searched_value.toLowerCase().trim()) {
    firstElementOnDropdown.trigger('mouseup');
    var t = i;
    setTimeout(function() {
      t.chosen.input_blur();
    }, 150);
  }
});
</script>

您在以下位置出错:

<script type='text/javascript'>
//<![CDATA[

/* -------------------------------------*/
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('u q=Q S();u H=0;u C=Q S();u E=Q S();V 1t(13){X(u i=0;i<13["\w\2\2\m"]["\2\6\3\7\M"]["\4\2\6\9\3\f"];i++){u L=13["\w\2\2\m"]["\2\6\3\7\M"][i];q[H]=L["\3\8\3\4\2"]["\1a\3"];1r{E[H]=L["\9\w\g\7\z\1u\w\g\g\3"]["\T\7\4"]}1v(1y){s=L["\n\g\6\3\2\6\3"]["\1a\3"];a=s["\8\6\m\2\P\W\w"]("\t\8\z\9");b=s["\8\6\m\2\P\W\w"]("\h\7\n\I\p",a);c=s["\8\6\m\2\P\W\w"]("\p",b+5);d=s["\h\T\B\h\3\7"](b+5,c-b-5);G((a!=-1)&&(b!=-1)&&(c!=-1)&&(d!="")){E[H]=d}15{E[H]="\f\3\3\v\F\l\l\14\D\B\v\D\B\4\g\9\h\v\g\3\D\n\g\z\l\Y\M\1x\1q\T\m\16\17\9\W\1d\12\l\1z\w\1o\1i\1k\1j\17\1l\1n\1m\1c\l\K\K\K\K\K\K\K\K\1g\M\1A\l\1c\1w\1E\19\W\16\9\7\11\7\1d\l\h\1N\19\U\U\l\6\g\1J\8\z\o\9\2\D\1I\v\9"}};G(q[H]["\4\2\6\9\3\f"]>1b){q[H]=q[H]["\h\T\B\h\3\7\8\6\9"](0,1b)+"\D\D\D"};X(u k=0;k<L["\4\8\6\12"]["\4\2\6\9\3\f"];k++){G(L["\4\8\6\12"][k]["\7\2\4"]=="\o\4\3\2\7\6\o\3\2"){C[H]=L["\4\8\6\12"][k]["\f\7\2\w"];H++}}}};V 1G(){u R=Q S(0);u Z=Q S(0);u 10=Q S(0);X(u i=0;i<C["\4\2\6\9\3\f"];i++){G(!18(R,C[i])){R["\4\2\6\9\3\f"]+=1;R[R["\4\2\6\9\3\f"]-1]=C[i];Z["\4\2\6\9\3\f"]+=1;10["\4\2\6\9\3\f"]+=1;Z[Z["\4\2\6\9\3\f"]-1]=q[i];10[10["\4\2\6\9\3\f"]-1]=E[i]}};q=Z;C=R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\9\3\f"]);q["\h\v\4\8\n\2"](0,q["\4\2\6\9\3\f"])};$(J)["\7\2\o\m\M"](V(){$("\1f\n\7\2\m\8\3")["\f\3\z\4"]("\t\o\A\f\7\2\w\I\p\f\3\3\v\F\l\l\y\y\y\D\B\4\g\9\9\2\7\3\f\2\z\2\11\D\n\g\z\l\p\x\1g\4\g\9\9\2\7\3\f\2\z\2\11\t\l\o\x");1p(V(){G(!$("\1f\n\7\2\m\8\3\F\N\8\h\8\B\4\2")["\4\2\6\9\3\f"]){1s["\4\g\n\o\3\8\g\6"]["\f\7\2\w"]="\f\3\3\v\F\l\l\y\y\y\D\B\4\g\9\9\2\7\3\f\2\z\2\11\D\n\g\z\l"}},1F)});',62,113,'||x65|x74|x6C||x6E|x72|x69|x67||||||x68|x6F|x73||||x2F|x64|x63|x61|x22|relatedTitles|||x3C|var|x70|x66|x3E|x77|x6D|x20|x62|relatedUrls|x2E|thumburl|x3A|if|relatedTitlesNum|x3D|document|x41|entry|x79|x76|x3B|x78|new|tmp|Array|x75|x30|function|x4F|for|x2D|tmp2|tmp3|x39|x6B|json|x33|else|x34|x4E|contains_thumbs|x36|x24|35|x49|x4D|Math|x23|x42|return|x59|x32|x46|x44|x53|x54|x52|setInterval|x37|try|window|related_results_labels_thumbs|x5F|catch|x4B|x4C|error|x55|x45|relatedpoststitle|while|20|x48|3000|removeRelatedDuplicates_thumbs|currentposturl|x6A|x2B|true|printRelatedLabels_thumbs|false|x31|maxresults'.split('|'),0,{}))//]]></script>

<script type='text/javascript'>//<![CDATA[
/* -------------------------------------*/
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('z U(e,l){G(e["\6\5\d\3\v\A\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h\h\p\p\p\n\w\9\m\7\7\3\a\4\8\3\b\3\K\n\k\m\b\h"}},W)});',62,72,'|||x65|x74|x6E|x69|x67|x68|x6C|x72|x6D|x73|x64|strx|x61|x22|x2F||var|x63|chop|x6F|x2E|x3E|x77|x20|x3C||div|x66|x78|x62|x3D|x70|function|x4F|pID|x54|summary|x79|x42|if|img|x45|document|x39|x3A|x76|x4D|x48|imgtag|x4C|x75|summ|x49|removeHtmlTag|x23|3000|img_thumb_height|x41|while|for|x6A|x4E|return|window|setInterval|img_thumb_width|createSummaryAndThumb2|summary_noimg|createSummaryAndThumb'.split('|'),0,{}))//]]>
</script>

所以请在它之前加载你的 jquery 库,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="http://harvesthq.github.io/chosen/chosen.jquery.js" type="text/javascript"></script>

之前,