Home >Web Front-end >JS Tutorial >How to assign value to span tag in javascript_javascript skills

How to assign value to span tag in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:29:342300browse

How to assign value to span tag in js? There are generally two methods:

The first method: output html

<body onload="s()">
<span id="hello"></span>
<script language="javascript">
 function s(){
document.getElementById("hello").innerHTML = "<iframe src= height=400 width=300></iframe>";
}
 </script>

Second method: output text

<body onload="s()">
<span id="hello"></span>
<script language="javascript">
 function s(){
document.getElementById("hello").innerText = "hello world";
}
</script>

Assign values ​​to multiple spans through jquery after the page is loaded

Since I want to display the current time in several places after the page is loaded, I need to assign values ​​to multiple spans.

The span code is written as follows:

<span name="currentDate"></span> (多个span) 

jQuery writing:

<script> 
$(document).ready(function() { 
var currentDate = new Date().toLocaleDateString(); 
$("span[name='currentDate']").each(function() { 
$(this).text(currentDate); 
}); 
}); 
</script> 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn