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 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>