Jquery adds and removes this

I like having an h3 header with few characters that say "expand me"

it will look like this: category xyz [+]

I like the function in jquery to chage / delete + to minus when clicked. I know how to add text to a string, but not how to delete it or just a part.

if you have a better approach feel free to tell me .. thanks!


note: implement the .remplace function ... but it doesn’t work properly, you can see it here ... the problem in which I used jquery 1.4 switches to 1.4.3, now it works!

+3
source share
7 answers

, +/- h3, , . :

$("body").delegate("#header a", "click", function(eventObj) {
    if ($(this).text().match(/\+/)) {
        $(this).text($(this).text().replace(/\+/, "-"));
    } else {
        $(this).text($(this).text().replace(/-/, "+"));
    }
});

, jsFiddle.

+2

.

HTML

<h3>Category XYZ
   <span id="toggle">
      <a href="#" class="expand" title="expand me">[+]</a>
      <a href="#" class="hide" title="hide me">[-]</a>
   </span>
</h3>
<div id="toggleContent">This is the text of category XYZ</div>

Javascript

$("span#toggle a").click(function() {
   $("div#toggleContent").toggle();
   $("span#toggle a").toggle();
});

CSS

div#toggleContent { display: none; }
span#toggle a.hide { display: none; }

JSFiddle, .

+3

$( "# idOfObject" ) HTML ( "[-]" );.

, , :)

0

jQuery html , :

$("#your-selector").text("category xyz[-]");
0

<span> <span> "class" "id". id Javascript, . , , , . , .

0

<span class=expandPlus>[+]</span>

-

$("h3").click(function() {
  $(this).children('expandPlus').html('[-]')
  .
  .
  .
});
0

/, , :

. jsFiddle.

a div. href - div.

<div id="container">
    <ul>
        <li>Category 01 <a href="#cat01">[+]</a></li>
        <li>Category 02 <a href="#cat02">[+]</a></li>
        <li>Category 03 <a href="#cat03">[+]</a></li>
    </ul>
    <br />
    <div id="categories">
        <div id="cat01">content 01</div>
        <div id="cat02">content 02</div>
        <div id="cat03">content 03</div>
    </div>
</div>

toggle, , , 1, , 0. , . , [-] [+]. , . , , a [+] [-].

$("#container ul > li > a").toggle(function (e) {
    var $this = $(this);
    $this.text("[-]"); // change the text
    // select content div using href as the selector
    $($this.attr("href")).css("display", "block");
    // prevent any other behavior
    e.preventDefault();
},
function (e) {
    var $this = $(this);
    $(this).text("[+]"); // switch to collapsed view
    $($this.attr("href")).css("display", null);
    e.preventDefault();
});
0

Source: https://habr.com/ru/post/1773737/


All Articles