将数组元素替换为id标记

时间:2015-07-02 17:19:23

标签: javascript jquery arrays

数组中的元素是以下代码的id标签,但它似乎不适用于此处。 每次调用高亮函数时,它都应该从先前的id标记中删除当前类,并且应该将类current指向作为id标记的下一个数组元素。

HTML CODE

<script type="text/javascript">
$(document).on("keydown", ".ui-cell-editor-input input", function(event) {
    if (event.keyCode == 13) {
        someName(); 
    }
});
</script>
   <p:remoteCommand name="someName" process="@this" update="@form" actionListener="#{invoiceBB.updatePrice}"/>

JAVASCRIPT CODE

{{1}}

3 个答案:

答案 0 :(得分:1)

正如@ lshettyl建议的那样,我也改为setInterval()来重播每个时间间隔:

var i = 0;

function highlight() {
  var point = ["one", "two", "three", "four", "five", "six", "seven"];
  $("#" + point[i]).removeClass("current"); //#point[i] doesn't seems to replace this thing

  i++
  if (i > 6) {
    i = 0;
  }
  $("#" + point[i]).addClass("current");
}
setInterval(highlight, 1000);

希望得到这个帮助。

答案 1 :(得分:1)

你能不能简单地这样做吗?

(function highlight() {
    setTimeout(function() {
        var $current = $("p.current").removeClass("current");
        var $next = $current.next().length && $current.next() || $current.siblings().first();
        $next.addClass("current");
        highlight();
    }, 1000);
}());

(function highlight() {
    setTimeout(function() {
        var $current = $("p.current").removeClass("current");
        var $next = $current.next().length && $current.next() || $current.siblings().first();
        $next.addClass("current");
        highlight();
    }, 1000);
}())
.current {
    color: green;
    font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p class="current" id="one">Point 1</p>
  <p id="two">Point 2</p>
  <p id="three">Point 3</p>
  <p id="four">Point 4</p>
  <p id="five">Point 5</p>
  <p id="six">Point 6</p>
  <p id="seven">Point 7</p>
</div>

答案 2 :(得分:0)

您需要重新运行突出显示并设置对前一元素的引用。

&#13;
&#13;
var $prior = null;
var point = ["one", "two", "three", "four", "five", "six", "seven"];
var i = 0;

function highlight() {
  if ($prior != null) $prior.removeClass("current");
  var id = '#' + point[i];
  
  i++;
  if (i > 6) {
    i = 0;
  }

  $(id).addClass("current");
  $prior = $(id);
  setTimeout(highlight, 500);
}

highlight();
&#13;
.current {
  background-color:#aacc66;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="current" id="one">Point 1</p>
<p id="two">Point 2</p>
<p id="three">Point 3</p>
<p id="four">Point 4</p>
<p id="five">Point 5</p>
<p id="six">Point 6</p>
<p id="seven">Point 7</p>
&#13;
&#13;
&#13;