这段代码在snippet中工作,但在blogger中不工作

This code is working in snippet but not in blogger

本文关键字:工作 但在 blogger snippet 段代码 代码      更新时间:2023-09-26

这是我的博客,我在博客中添加了如下代码。下面的代码在代码段中正常工作,就像下面的例子一样,但它在我的博客中不正常工作,如果我使用任何jquery库使其工作,我会在博客平台中使用它。请参阅下面的代码段,它在代码段中运行良好。我的目标是,如果另一个小部件显示,但在我的博客中同时显示了两个小部件,则隐藏一个小程序。示例小提琴工作良好

<script type="text/javascript">
var control1VisibleCheck = function () {
  var now = new Date();
  //TODO: modify this logic to your needs: have a look on the Date() object's members and methods to implement what you need
  if (now.getSeconds() % 20 == 0)    //I'd like to show control1 on even minutes
    return true;
  return false;
}
if (control1VisibleCheck())
  document.getElementById('multi-search-groups').style.display = 'none';
else
  document.getElementById('multi-search').style.display = 'none';
</script>

<div id="multi-search">
  <select id="cmbColumn" name="cmbColumn">
    <option value="" />Columns
    <option value="apple+" />apple
    <option value="grapes+" />grapes
  </select>
  <select id="cmbSidebar" name="cmbSidebar">
    <option value="" />Sidebars
    <option value="mango+" />mango
    <option value="berry+" />berry
  </select>
</div>
<div id="multi-search-groups">
  <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>
<!--div id="control1" style="width: 200px; height: 100px; background-color: red;">
</div>
<div id="control2" style="width: 200px; height: 100px; background-color: green;">
</div-->

以下代码运行良好

var control1VisibleCheck = function () {
  var now = new Date();
  //TODO: modify this logic to your needs: have a look on the Date() object's members and methods to implement what you need
  if (now.getSeconds() % 2 == 0)    //I'd like to show control1 on even minutes
    return true;
  return false;
}
if (control1VisibleCheck())
  document.getElementById('multi-search-groups').style.display = 'none';
else
  document.getElementById('multi-search').style.display = 'none';
<div id="multi-search">
  <select id="cmbColumn" name="cmbColumn">
    <option value="" />Columns
    <option value="apple+" />apple
    <option value="grapes+" />grapes
  </select>
  <select id="cmbSidebar" name="cmbSidebar">
    <option value="" />Sidebars
    <option value="mango+" />mango
    <option value="berry+" />berry
  </select>
</div>
  
<div id="multi-search-groups">
  <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>
<!--div id="control1" style="width: 200px; height: 100px; background-color: red;">
</div>
<div id="control2" style="width: 200px; height: 100px; background-color: green;">
</div-->

似乎你的小提琴也不能正常工作,正如评论中所建议的那样,在页面(文档)准备好后执行你的JavaScript:

JavaScript文档就绪功能

(function() {
    // your code here
});

完整的JavaScript:

(function() {
    var control1VisibleCheck = function () {
      var now = new Date();
      //TODO: modify this logic to your needs: have a look on the Date() object's members and methods to implement what you need
      if (now.getMinutes() % 2 == 0)    //I'd like to show control1 on even minutes
        return true;
      return false;
    }
    if (control1VisibleCheck())
      document.getElementById('multi-search-groups').style.display = 'none';
    else
      document.getElementById('multi-search').style.display = 'none';
});

工作JSFiddle

已解决!我在</div>标签之后添加了我的<script>,它的工作原理如下,这是我在博客中得到的代码。

我得到了我想要的结果,我的意思是,如果一个小部件被隐藏,另一个小程序被显示出来,我使用了if (now.getSeconds() % 2 == 0),下面是我在博客中添加的完整代码。我刚刚改变了,我的意思是我在div之后添加了script标签,它起作用了。

<div id="multi-search">
  <select id="cmbColumn" name="cmbColumn">
    <option value="" />Columns
    <option value="apple+" />apple
    <option value="grapes+" />grapes
  </select>
  <select id="cmbSidebar" name="cmbSidebar">
    <option value="" />Sidebars
    <option value="mango+" />mango
    <option value="berry+" />berry
  </select>
</div>
<div id="multi-search-groups">
  <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">
var control1VisibleCheck = function () {
  var now = new Date();
  //TODO: modify this logic to your needs: have a look on the Date() object's members and methods to implement what you need
  if (now.getSeconds() % 2 == 0)    //I'd like to show control1 on even minutes
    return true;
  return false;
}
if (control1VisibleCheck())
  document.getElementById('multi-search-groups').style.display = 'none';
else
  document.getElementById('multi-search').style.display = 'none';
</script>
相关文章: