Java script

[Java] Math.random() String.replace() Element.getAttribute(); new Date()

EmilyP 2021. 3. 24. 17:27


Math Object;

Math.random();

<h2>JavaScript Math</h2>

 

<p>Math.floor (Math.random () * 10)은 0에서 9 사이의 임의의 정수를 반환함 .:</p>

 

<p id="demo"></p>

 

<script>

document.getElementById("demo").innerHTML = Math.floor(Math.random() * 10);

</script>

 

String Object:

String.replace();

 

    <h2>JavaScript String Methods</h2>

    

    <p>Replace "경일아카데미" with "블록체인반" in the paragraph below:</p>

    

    <button onclick="myFunction()">Try it</button>

    

    <p id="demo">경일아카데미에서 코딩배워보세요!</p>

    

    <script>

    function myFunction() {

        var str = document.getElementById("demo").innerHTML

        var txt = str.replace("경일아카데미","블록체인반");

        document.getElementById("demo").innerHTML = txt;

    }

    </script>

Document Object;

Element.getAttribute();

getAttribute1

    <body>

        <p><a id="abc" href="https://www.codingfactory.net">Coding Factory</a></p>

        <script>

          var jbHref = document.getElementById'abc' ).getAttribute'href' );

          document.write'<p>href : ' + jbHref + '</p>' );

        </script>

      </body>

getAttribute2

<body>

 

    <h1 class="democlass">Hello World</h1>

    

    <p>Click the button to display the value of the class attribute of the h1 element.</p>

    

    <button onclick="myFunction()">Try it</button>

    

    <p id="demo"></p>

    

    <script>

    function myFunction() {

      var x = document.getElementsByTagName("H1")[0].getAttribute("class"); 

      document.getElementById("demo").innerHTML = x;

    }

    </script>

    

    </body>

getAttribute3

<body>

 

    Read about the <a id="myAnchor" href="dom_obj_attributes.asp" target="_blank">Attr object</a>.

    

    <p>Click the button to display the value of the target attribute of the link above.</p>

    

    <button onclick="myFunction()">Try it</button>

    

    <p id="demo"></p>

    

    <script>

    function myFunction() {

      var x = document.getElementById("myAnchor").getAttribute("target");

      document.getElementById("demo").innerHTML = x;

    }

    </script>

    

    </body>

getAttribute4

<body>

<p>Click the button to display the value of the onclick attribute of the button element.</p>

<button id="myBtn" onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myBtn").getAttribute("onclick");
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>

new Date

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript new Date()</h2>

<p id="demo"></p>

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

'Java script' 카테고리의 다른 글

[Java] console에 번호 순서대로 찍기  (0) 2021.03.30
[Java] Rolling banner  (0) 2021.03.25
[Java] document.  (0) 2021.03.24
[Java] 팝업열기  (0) 2021.03.24
[Java] 많은 수 곱하기 값 구하기  (0) 2021.03.24