English 中文(简体)
jquery 多重和变化的 ids 选择器
原标题:jquery multiple and changing ids selectors

我得到了一个简单的天赐, 我想显示和隐藏我的divs 根据他们的异体, 但无法找到,例如,我有一个不同的divs 和输入 与不同的IDs:

<input onfocus="showtip(1)" onblur="hidetip(1); />
<div id="tip1"></div>
<input onfocus="showtip(2)" onblur="hidetip(2); />
<div id="tip2"></div>

function showtip(id) {
    $( .tip[id] ).show( slow );
}
function hidetip(id) {
    $( .tip[id] ).hide( slow );
}

但我无法用动画来掩盖它, 所以我用了jquery, 但我无法定义ids: (谢谢大家的帮助! 我真的很感激! )

最佳回答

你是说:

function showtip(id) {
    $( # +tip).show( slow );
    //OR
    $( yourElement[id=" +tip+ "] ).show( slow );
}
问题回答

要在 jQuery 中使用 < code> id 属性, 请使用 < a href=" "http://api.jquery.com/id- selector/" rel="nofol" >id selfor :

$( #<yourid> ).show( slow );

所以,你的方法将是:

function showtip(id) {
   $( #tip +id).show( slow );
}

(注: ids 在整个文档中应该是独一无二的,所以不必在选择器中包括 div bit)

如果您 id 属性包含字符, 如 . , 意思是其它事物( 即类选择器), 那么您就可以使用属性等于选择器 :

$( [id="123.abc"] ).show( slow )

@ ManseUK s 方法是最常见的方法。 但技术上是不正确的, 例如, 它对于包含 CSS 中具有其他含义的字符的 ID 将失败, 如 < code>. 。 最正确的解决方案是 :

function showTip(id) {
    $(document.getElementById(id)).show("slow");
}

// This will work:
<div id="abc.123"></div>
showTip("abc.123");

这个解决方案也略微快一些, 因为从内部看 jQuery 将把 $( "#" + id) 翻译成( 在工作的情况下) 。

如何运作 :

  • document.getElementById(id) gets the raw DOM node.
  • $(...) wraps a raw DOM node in a jQuery object, which has jQuery methods like show.




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签