简体   繁体   English

使用 jQuery 更改文本

[英]Changing Text Using jQuery

What I'm trying to do is add a [+] or [-] sign to an expandable heading so that once the user clicks on the heading, its content part will be shown and its sign part will change from + to - and vice versa.我想要做的是向可扩展标题添加[+][-]符号,这样一旦用户单击标题,就会显示其内容部分,并且其符号部分将从+变为- ,反之亦然反之亦然。 Since there are multiple headings, I used jQuery's next() .由于有多个标题,我使用了 jQuery 的next() So far the .content toggling works well, but for some reason the sign is not changing.到目前为止, .content切换效果很好,但由于某种原因,标志没有改变。

<script type="text/javascript">
jQuery(document).ready(function() {
  jQuery(".content").hide();

  jQuery(".heading").click(function()
  {
    jQuery(this).next(".content").toggle();
    if (jQuery(this).next(".sign").text()=="[+]")
    {jQuery(this).next(".sign").text("[-]");}
    else
    {jQuery(this).next(".sign").text("[+]");}

  });
});
</script>

for (int i = 0; i < nodes.getLength(); i++)
 {
   Element element = (Element) nodes.item(i);
   NodeList title = element.getElementsByTagName("Title");
   Element ttl = (Element) title.item(0);
   String linkTitle = getCharacterDataFromElement(ttl);
   htmlReturn += "<a href='#' class='heading'><h4>" + linkTitle + " <span class='sign'>[+]</span></h4></a>";
   htmlReturn += "<div class='content'>";

    ...

}

Because the sign is a descendant element of the heading, not a sibling, use find instead of next :因为符号是标题的后代元素,而不是兄弟元素,所以使用find而不是next

var sign = jQuery(this).find(".sign");
sign.text(sign.text() === "[+]" ? "[−]" : "[+]");
// Note: A minus sign (−) has the same width as a plus sign (+),
// whereas a hyphen (-) is typically narrower.
if (jQuery(".sign").text()=="[+]")
{jQuery(".sign").text("[-]");}
else
{jQuery(".sign").text("[+]");}

Well, next() only gets the immediate sibling.好吧, next()只获取直接兄弟。 Try using closest() instead.尝试改用closest()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM