سؤال

I need to sort a jQuery tablesorter by a unix timestamp rather than an unpredictable displayed date that depends upon locale, so I want to try a possible solution where a hidden span can be used to sort the date column.

How can an element be inserted in front of text or at the beginning of another element while retaining the existing text contents in the containing element?

Basic intent

Given this element:

<td id="myTD">Difficult date to parse</td>

I'd like to turn it into this:

<td id="myTD">
    <span style="display:none">1398019663</span>
    Difficult date to parse
</td>
هل كانت مفيدة؟

المحلول

This looks like a job for HTML5 data attributes, as in :

<td id="myTD" data-sortkey="1398019663">
    Difficult date to parse
</td>

You can get and set the value of data attributes using the data function in JQuery.

نصائح أخرى

You could store the value in a temporary variable, then append that value into whatever you need to insert.

JSfiddle for example: http://jsfiddle.net/kWzwF/2/

e.g:

var myVal = $('h1').text()
$('h1').html('<span>newstuff </span>' + myVal);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top