Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính sự kiện onselect trong HTML

Thuộc tính sự kiện onselect trong HTML được kích hoạt khi người dùng chọn một số văn bản của một phần tử HTML trong tài liệu HTML.

Cú pháp

Cú pháp như sau:

<tagname onselect="script"></tagname>

Ví dụ

Dưới đây là ví dụ minh họa thuộc tính sự kiện onselect trong HTML:

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
      padding: 20px;
    }
    textarea {
      border: 2px solid #fff;
      background: transparent;
      padding: 10px;
    }
    ::placeholder {
      color: #000;
    }
  </style>
</head>
<body>
<h1>Demo thuộc tính sự kiện onselect trong HTML</h1>
<textarea onselect="selectFn()" rows="5" cols="50">Vui lòng chọn tôi vì tôi là văn bản giả.</textarea>
<script>
  function selectFn() {
    document.body.style.background = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
    document.querySelector("textarea").style.background = "#fff";
  }
</script>
</body>
</html>

Kết quả