显示/隐藏输入表单中指定 id 的 div
我在页面上有很多带有如下 id 的 div:
<div id="s001">, <div id="s002">, <div id="s003">....<div id="s050">...<div id="s200">
默认情况下,所有这些 div 都是隐藏的。 有文本输入表格。用户在文本区域中输入类似 s005
的内容,并且出现 id=s005
的 div。如果下一个输入是 s101
— 只有 div s101
变得可见,div s005
再次隐藏。如果输入没有在 textarea 中指定 id 的 div — 我们只是不显示任何内容。
所以我需要从我的 div 中应用和删除类,但我不确定如何使用 addClass 或toggleClass 我对 jQuery 很陌生,但我对这种情况进行了研究并提出了这些:
head:
<style type="text/css">
.hidden { display: none }
.shown { display: block }
</style>
body:
<input type="text" size="4">
<script>
$("input").keyup(function () {
var value = $(this).val();
$(#value).toggleClass("shown", addOrRemove);
}).keyup();
</script>
<div id="s001" class="hidden">s001 contents</div>
<div id="s002" class="hidden">s002 contents</div>
<div id="s003" class="hidden">s003 contents</div>
<div id="s004" class="hidden">s004 contents</div>
<div id="s005" class="hidden">s005 contents</div>
我确信这个字符串是错误的:
$(#value).toggleClass("shown", addOrRemove);
但我不知道我应该更改什么。我认为主要的复杂性是在输入表单中的文本更改后必须删除 shown
类。一次只能显示一个div! 提前感谢大家,很抱歉成为菜鸟。
I got a lot of divs on page with ids like these:
<div id="s001">, <div id="s002">, <div id="s003">....<div id="s050">...<div id="s200">
By default all these divs are hidden.
There is text input form. User types something like s005
in textarea and div with id=s005
appear. If the next input is s101
— only div s101
becomes visible and div s005
is hidden again. If input is there is no div with id specified in textarea — we just dislay nothing.
So I need to apply and remove classes from my divs, but I'm not sure how — with addClass or toggleClass
I am pretty new to jQuery, but I did research for this case and come up with these:
head:
<style type="text/css">
.hidden { display: none }
.shown { display: block }
</style>
body:
<input type="text" size="4">
<script>
$("input").keyup(function () {
var value = $(this).val();
$(#value).toggleClass("shown", addOrRemove);
}).keyup();
</script>
<div id="s001" class="hidden">s001 contents</div>
<div id="s002" class="hidden">s002 contents</div>
<div id="s003" class="hidden">s003 contents</div>
<div id="s004" class="hidden">s004 contents</div>
<div id="s005" class="hidden">s005 contents</div>
I am sure that this string is wrong:
$(#value).toggleClass("shown", addOrRemove);
But I have no idea what should I change. I believe that the main comlication is that class shown
must be removed after text in input form is changed. Only one div must be displayed at one time!
thanks everyone in advance and sorry for being noob.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个正在运行的示例。唯一没有发生的是对函数“addOrRemove”的调用。
http://jsfiddle.net/pVEus/
希望这可以帮助您入门。
鲍勃
Here is an example that is working. The only thing that is not happening is the call to the function 'addOrRemove'.
http://jsfiddle.net/pVEus/
Hope this gets you started.
Bob
应该做的伎俩
Should do the trick
尝试以下操作(请注意,我还为您的文本框提供了一个 id,这样当屏幕上有其他文本框时,它就不会爆炸):
Try the following (notice I also gave your textbox an id so this doesn't blow up when you have other textboxes on the screen):