[英]Swap position of two strings in a div
The current date format of a page is 页面的当前日期格式为
maart 2 2015
The HTML created for this is: 为此创建的HTML是:
<span class="entry-time">
<span class="updated" style="display:none;">2015-03-06T09:24:26+00:00</span>
<time class="published" datetime="2015-03-02T16:58:01+00:00">maart 2 2015</time>
</span>
What I want to do is swtich the places of maart and 2 so that the string reads "2 maart 2015". 我想要做的是旋转maart和2的位置,以便字符串显示为“ 2 maart 2015”。
Is this possible in jquery? 在jQuery中这可能吗?
As far as I know, you just need to make some treatment using the native split() method: 据我所知,您只需要使用本机split()方法进行一些处理:
var str = "maart 2 2015";
var result = str.split(" ");
Gives : 给出:
maart,2,2015
Then you just need to recreate a new string from this array in the order of your choice. 然后,您只需要按照选择的顺序从该数组重新创建一个新字符串。
That's not a problem at all. 那根本不是问题。 For example this line would do the job:
例如,以下行将完成这项工作:
var splitted = $(".published").html().split(" ");
$(".published").html(splitted[1] + " " + splitted[0] + " " + splitted[2]);
What we do here is using the first .html
to give all the elements with the class .published
(make sure you don't have unwanted "published" elements) a new inner html data. 我们在这里使用的是第一个
.html
,为类.published
所有元素(确保您没有不必要的“ published”元素)赋予新的内部html数据。 We provide a string which is the new text in the tag. 我们提供一个字符串,它是标记中的新文本。 We split the string by space and have a array with three elements (maart, 2, 2015).
我们将字符串按空间分割,并得到一个包含三个元素的数组(maart,2015年2月)。 Now we simply have to reverse the order (1, 0, 2) and we're done.
现在我们只需要颠倒顺序(1、0、2)就可以了。
I assume this is in wordpress? 我认为这是在WordPress中? If it is, go to settings -> general and set your timezone if it is not correct.
如果是,请转到设置->常规,如果不正确,请设置您的时区。 Then below you can change the format by selecting one or by creating one http://codex.wordpress.org/Formatting_Date_and_Time
然后在下面,您可以通过选择一种格式或通过创建一种http://codex.wordpress.org/Formatting_Date_and_Time来更改格式
it should then go as you wan't if the theme is developed in a good way 如果主题发展得很好,就可以随心所欲
You can separate it into an array by spaces then switch the position of the words like so: 您可以用空格将其分成一个数组,然后像这样切换单词的位置:
var array = $(".published").text().split(" ");
$(".published").text(array[1]+" "+array[0]+" "+array[2])
Or you can just change the text: 或者,您可以只更改文本:
$(".published").text("2 maart 2015")
So this is the script I used eventually: 这是我最终使用的脚本:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$( document ).ready(function() {
$(".published").html(
$(".published").html().split(" ")[1] + " " +
$(".published").html().split(" ")[0] + " " +
$(".published").html( ).split(" ")[2]
);
});
</script>
Thanks a lot for your help! 非常感谢你的帮助!
Here a fiddle: http://jsfiddle.net/qr1zzp3b/ 这里是一个小提琴: http : //jsfiddle.net/qr1zzp3b/
var months = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var date = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();
console.log(date + " " + months[month]
+ " " + year);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.