JQuery基于数组内容设置显示属性

时间:2017-10-27 06:09:21

标签: javascript jquery

我正在尝试使用我拥有的旧代码并将其转换为JQuery,但我似乎无法正确地理解任何指导。

var termarray = ["help", "help me", "-h", "--help"];
commandterm = $("#search-bar").val();

document.getElementById('helpblock').style.display = ~termarray.indexOf(commandterm.toLowerCase()) ? "block" : "none";

这就是我目前在JavaScript中所拥有的。

编辑1:

对不起,我没有说清楚。这是旧的JavaScript代码

document.getElementById('helpblock').style.display = ~termarray.indexOf(commandterm.toLowerCase()) ? "block" : "none";

我想要做的就是把它变成JQuery。它应该做的基本上总结如下:

如果termarray包含commandterm,则将helpblock display设置为block,如果没有,则将其设置为none。

我可以通过If语句实现这一点,但我觉得它并不过分高效,因为我将编写一些这些行用于文本检测。

对于JQuery替代方案,我不完全确定在等于之后从哪里开始,所以现在我有:

$('#kws').css('display') = 

除此之外,我不太确定......对于缺乏描述我道歉。

3 个答案:

答案 0 :(得分:0)



var termarray = ["help", "help me", "-h", "--help"];
console.log(~termarray.indexOf("help"));
console.log(~termarray.indexOf("help") ? "block" : "none");
console.log(~termarray.indexOf("-m"));
console.log(~termarray.indexOf("-m") ? "block" : "none");




您的逻辑正常运行。

在显示document.getElementById('helpblock').style.display

时,检查您是否选择了正确的元素

$("#search-bar").val();正在返回正确的值。

答案 1 :(得分:0)

如果您希望display设置为block,如果数组中存在commandterm,则将indexOf与-1进行比较

document.getElementById('helpblock').style.display = termarray.indexOf(commandterm.toLowerCase())!=-1 ? "block" : "none";

-1不被视为错误

请查看以下演示:



var x = "a";
x = ["somestring"].indexOf("someotherstring") ? "exists" : "doesnotexists";
console.log(x);




答案 2 :(得分:0)

import numpy as np
l = range(50,80)
np.random.choice(l, 100, True)