English 中文(简体)
Jquery subtracting 1 from a Value with.text and Maintenance html
原标题:Jquery subtracting 1 from a value with .text and keeping html

I m 试图将1(一)从价值中分离出来。 我设法这样做:

$( .notis ).text(parseInt($(".notis").text()) - 1);

问题在于:<代码><a> 。

<a href="#" class="notis"><img src="/images/icons/picture.png">3</a>

因此,上面的分类将从价值中减去1(一个),但也消除形象! 我怎么能够这样做?

最佳回答

You can store the image in a temporary variable, then add it to the node using the prepend method:

$( .notis ).each(function() {
    var $this = $(this);                        // <-- Reference to current elem
    var $img = $this.find( img );               // <-- Reference to <img>
    $this.text(parseInt($this.text(), 10) - 1); // <-- Your logic
    $this.prepend($img);                        // <-- Add image again.
});

Demo: http://jsfiddle.net/sPnhL/

附加说明:

  • Use parseInt( ... , 10) instead of parseInt( ... ). Numbers prefixed by a zero will be treated inconsistently across browsers. By specifygin the radix of 10, the number will be parsed correctly.
  • $( .notis ) selects all elements with class notis. When you re using $( notis ).text($( notis ).text()-1), all elements with class notis will contain the value of the first .notis element (because $( .notis ).text() returns the value of the first .notis). That s why I used .each in my code.
  • When you re going to use $(this) more than once, it s worth storing the variable $(this) in a temporary variable, for improved efficiency.
问题回答

也许可以找到更好的解决办法,但是,在你修改案文之后,你可以挽救儿童联系内容,重新引导他们:

$( .notis ).each(function (index, element) {
    var $children = $(this).children();
    $(this).text(parseInt($(this).text()) - 1).prepend($children);
});

http://jsfiddle.net/epuxy/“rel=“nofollow” http://jsfiddle.net/epuxy/。 (在你点击该链接时,见上下层变化的数字)

I d suggest to wrap up the number in a <span> and replace its content.

在干燥的或跨过的(旧的)班级中打3,并从那里取。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签