Programing

javascript : 텍스트 선택 비활성화

crosscheck 2020. 11. 3. 07:39
반응형

javascript : 텍스트 선택 비활성화


내 웹리스트에서 텍스트 선택을 비활성화하기 위해 javascript를 사용하고 있습니다.

코드는 다음과 같습니다.

<script type="text/JavaScript">

function disableselect(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function ("return false")

if (window.sidebar) {
  document.onmousedown = disableselect
  document.onclick = reEnable
}
</script>

유사한 스크립트는 여기 에서 찾을 수 있습니다 .

내 로컬 호스트에서 : 모든 브라우저 (Firefox, Chrome, IE 및 Safari)가 잘 작동합니다.

내 라이브 사이트에서 : Firefox를 제외하고 모두 괜찮습니다.

내 질문은 다음과 같습니다.

  1. 누구든지 Firefox가 라이브 사이트와 로컬 호스트에 대해 다르게 작동하는 이유에 대한 제안이 있습니까? 참고 : Javascript가 활성화됩니다.

  2. 내 스크립트가 너무 단순 해서 똑같은 결과로 다음시도했을 수도 있습니다.


이 CSS 방법을 사용하십시오.

body{
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

여기에서 동일한 답을 찾을 수 있습니다. CSS를 사용하여 텍스트 선택 강조 표시를 비활성화하는 방법?


드래그 기능을 제공하기 위해 슬라이더 UI 컨트롤을 작성하고 있습니다. 이것은 사용자가 드래그 할 때 콘텐츠가 선택되지 않도록하는 방법입니다.

function disableSelect(event) {
    event.preventDefault();
}

function startDrag(event) {
    window.addEventListener('mouseup', onDragEnd);
    window.addEventListener('selectstart', disableSelect);
    // ... my other code
}

function onDragEnd() {
    window.removeEventListener('mouseup', onDragEnd);
    window.removeEventListener('selectstart', disableSelect);
    // ... my other code
}

bind startDrag on your dom:

<button onmousedown="startDrag">...</button>

If you want to statically disable text select on all element, execute the code when elements are loaded:

window.addEventListener('selectstart', function(e){ e.preventDefault(); });

For JavaScript use this function:

function disableselect(e) {return false}
document.onselectstart = new Function (return false)
document.onmousedown = disableselect

to enable the selection use this:

function reEnable() {return true}

and use this function anywhere you want:

document.onclick = reEnable

If you got a html page like this:

    <body
    onbeforecopy = "return false"
    ondragstart = "return false" 
    onselectstart = "return false" 
    oncontextmenu = "return false" 
    onselect = "document.selection.empty()" 
    oncopy = "document.selection.empty()">

There a simple way to disable all events:

document.write(document.body.innerHTML)

You got the html content and lost other things.


Simple Copy this text and put on the before </body>

function disableselect(e) {
  return false
}

function reEnable() {
  return true
}

document.onselectstart = new Function ("return false")

if (window.sidebar) {
  document.onmousedown = disableselect
  document.onclick = reEnable
}

One might also use:

onselectstart = (e) => {e.preventDefault()}

Example:

onselectstart = (e) => {
  e.preventDefault()
  console.log("nope!")
  }
Select me!

One other alternative, by testing CSS supports, and disable user-select:

let FF
if (CSS.supports("( -moz-user-select: none )")){FF = 1} else {FF = 0}
(FF===1) ? document.body.style.MozUserSelect="none" : document.body.style.userSelect="none"

참고URL : https://stackoverflow.com/questions/16805684/javascript-disable-text-select

반응형