سؤال

هذا السؤال لديه بالفعل إجابة هنا:

أرغب في إضافة نص إلى صفحة الويب الخاصة بي كتسمية وجعلها غير قابلة للتحديد.

بمعنى آخر ، عندما يكون مؤشر الماوس فوق النص ، أود ألا يتحول إلى نص يختار المؤشر على الإطلاق.

مثال جيد على ما أحاول تحقيقه هو الأزرار الموجودة على هذا الموقع (الأسئلة ، العلامات ، المستخدمون ، ...)

هل كانت مفيدة؟

المحلول

لا يمكنك القيام بذلك باستخدام HTML الفانيليا العادي ، لذلك لا يمكن لـ JSF فعل الكثير من أجلك هنا أيضًا.

إذا كنت تستهدف متصفحات لائقة فقط ، ثم فقط الاستفادة من CSS3:

.unselectable {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
<label class="unselectable">Unselectable label</label>

إذا كنت ترغب في تغطية المتصفحات القديمة أيضًا ، ففكر في عودة JavaScript هذه:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2310734</title>
        <script>
            window.onload = function() {
                var labels = document.getElementsByTagName('label');
                for (var i = 0; i < labels.length; i++) {
                    disableSelection(labels[i]);
                }
            };
            function disableSelection(element) {
                if (typeof element.onselectstart != 'undefined') {
                    element.onselectstart = function() { return false; };
                } else if (typeof element.style.MozUserSelect != 'undefined') {
                    element.style.MozUserSelect = 'none';
                } else {
                    element.onmousedown = function() { return false; };
                }
            }
        </script>
    </head>
    <body>
        <label>Try to select this</label>
    </body>
</html>

إذا كنت تستخدم بالفعل jQuery, ، ثم إليك مثال آخر يضيف وظيفة جديدة disableSelection() إلى jQuery حتى تتمكن من استخدامه في أي مكان في رمز jQuery الخاص بك:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2310734 with jQuery</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>
            $.fn.extend({ 
                disableSelection: function() { 
                    this.each(function() { 
                        if (typeof this.onselectstart != 'undefined') {
                            this.onselectstart = function() { return false; };
                        } else if (typeof this.style.MozUserSelect != 'undefined') {
                            this.style.MozUserSelect = 'none';
                        } else {
                            this.onmousedown = function() { return false; };
                        }
                    }); 
                } 
            });

            $(document).ready(function() {
                $('label').disableSelection();            
            });
        </script>
    </head>
    <body>
        <label>Try to select this</label>
    </body>
</html>

نصائح أخرى

لم ينشر أحد هنا إجابة مع جميع أشكال CSS الصحيحة ، لذلك هنا:

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

إن الحل الحديث الكامل لمشكلتك يعتمد على CSS بحت ، ولكن لاحظ أن المتصفحات القديمة لن تدعمه ، حيث تحتاج الحالات التي تحتاج إلى العودة إلى حلول مثل الآخرين.

لذلك في CSS النقي:

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;

ومع ذلك ، فإن مؤشر الماوس سيظل يتغير إلى سيارة إلكترونية عندما تكون فوق نص العنصر ، لذلك تضيف إلى ذلك:

cursor: default;

CSS الحديثة أنيقة جدا.

قمت بتغيير المكون الإضافي jQuery المنشور أعلاه بحيث يعمل على العناصر الحية.

(function ($) {
$.fn.disableSelection = function () {
    return this.each(function () {
        if (typeof this.onselectstart != 'undefined') {
            this.onselectstart = function() { return false; };
        } else if (typeof this.style.MozUserSelect != 'undefined') {
            this.style.MozUserSelect = 'none';
        } else {
            this.onmousedown = function() { return false; };
        }
    });
};
})(jQuery);

ثم يمكنك ذلك شيء مثل:

$(document).ready(function() {
    $('label').disableSelection();

    // Or to make everything unselectable
    $('*').disableSelection();
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top