最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to replace text with another text in javascript on page load? - Stack Overflow

programmeradmin0浏览0评论

I have an html inspect code as shown below:

<time datetime="07:05 13-06-2020" data-area="ab">13 June <span>07:05</span></time>

The above html inspect prints the following on webpage:

13 June

What I want to achieve is instead of 13 June, it should display 13 Juin on page load.

This is what I have tried but its replacing the 13 June with Juin.

$(document).ready(function(){ 
    $("time").replaceWith("Juin");
});

I have an html inspect code as shown below:

<time datetime="07:05 13-06-2020" data-area="ab">13 June <span>07:05</span></time>

The above html inspect prints the following on webpage:

13 June

What I want to achieve is instead of 13 June, it should display 13 Juin on page load.

This is what I have tried but its replacing the 13 June with Juin.

$(document).ready(function(){ 
    $("time").replaceWith("Juin");
});
Share Improve this question asked Jun 14, 2020 at 2:07 user1950349user1950349 5,15620 gold badges73 silver badges128 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

You are replacing the entire time element by using .replaceWith. Instead, you need to change the inner HTML of the time tag:

$(document).ready(function() {
  $("time").html($("time").html().replace('June', 'Juin'));
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<time datetime="07:05 13-06-2020" data-area="ab">13 June <span>07:05</span></time>

One more way how you can do that

window.onload = function(){
   var a = document.getElementsByTagName('time')[0];
   a.innerHTML = a.innerHTML.replace(/June/,'Juin');
};
<time datetime="07:05 13-06-2020" data-area="ab">13 June <span>07:05</span></time>

Try this:

Demo: https://jsfiddle/o60x7q83/

$(document).ready(function(){ 
    $("time").html($("time").html().replace('June', 'Juin'));
});

low explained about "Javascript" in Jquery is other syntax onload can load on body as reference to this function

(function(e) {
e.preventDefault;

 function startTime() {
 var today = new Date();
 var h = today.getHours();
 var m = today.getMinutes();
 var s = today.getSeconds();
// add a zero in front of numbers<10
 m = checkTime(m);
 s = checkTime(s);
document.getElementById("txt").innerHTML = h + ":" + m + ":" + s;
var t = setTimeout(function(){ startTime() }, 500);
}

function checkTime(i) {
if (i < 10) {
 i = "0" + i;
 }
 return i;
 }
}())
发布评论

评论列表(0)

  1. 暂无评论