简体   繁体   English

使用w3c中的javascript验证onselectstart

[英]validating onselectstart with javascript in w3c

I'm really very new to javascript, and trying to validate a page to xhtml transitional. 我对javascript真的很陌生,并尝试验证页面到xhtml过渡。 I use onselectstart="return false" 我使用onselectstart =“ return false”

So I understand that I am wanting to create a javascript function that will insert that as an id. 因此,我了解到我想创建一个JavaScript函数,将其作为ID插入。 I even found this http://www.webmasterworld.com/javascript/3054096.htm and he figured out how to do it. 我什至发现了这个http://www.webmasterworld.com/javascript/3054096.htm ,他想出了办法。

He is putting the onload in the body and setting the ids. 他将负担放在体内并设置ID。 Can I do this with a class and not set specific ID numbers? 我可以使用课程而不设置特定的ID号吗?

You can use the document.getElementsByClassName method, but it isn't standard yet (it will be part of HTML5 ), you can be completely sure that it will not work on any IE version, some modern browsers provide a native implementation, but if it isn't available, a loop checking for the specific class you look for can be done. 您可以使用document.getElementsByClassName方法,但是它不是标准方法(它将是HTML5的一部分),可以完全确保它不适用于任何IE版本, 某些现代浏览器提供了本机实现,但是如果它不可用,可以对您要查找的特定类进行循环检查。

I personally use the following function, inspired by the Dustin Diaz implementation: 我个人是受达斯汀·迪亚兹Dustin Diaz)实现启发而使用以下功能:

function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available
    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null )
          node = document;
        var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}

Then you can use it like this: 然后,您可以像这样使用它:

window.onload = function () {
  var returnFalse = function () { return false; },
      els = getElementsByClassName(document, 'yourClassName'),
      n = els.length;

  while (n--) {
    els[n].onselectstart = returnFalse; 
  }
};

EDIT - This modified answer incorporates my previous answer with the answer provided by CMS. 编辑 -此修改后的答案将我以前的答案与CMS提供的答案结合在一起。

This code works in IE 6/7/8: 此代码在IE 6/7/8中有效:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>test page</title>
    <script type="text/javascript" language="javascript">
        window.onload = function() {
            var elements = getElementsByClassName(document, 'noselect');
            for (var i = 0; i < elements.length; i++) {
                elements[i].attachEvent('onselectstart', rfalse);
            }
        }

        function rfalse() { return false; }

        function getElementsByClassName(node, classname) {
            if (node.getElementsByClassName) { // use native implementation if available
                return node.getElementsByClassName(classname);
            } else {
                return (function getElementsByClass(searchClass, node) {
                    if (node == null)
                        node = document;
                    var classElements = [],
                els = node.getElementsByTagName("*"),
                elsLen = els.length,
                pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)"), i, j;

                    for (i = 0, j = 0; i < elsLen; i++) {
                        if (pattern.test(els[i].className)) {
                            classElements[j] = els[i];
                            j++;
                        }
                    }
                    return classElements;
                })(classname, node);
            }
        }
    </script>
</head>
<body>
    <h2 class='noselect'>
        this text cannot be selected
    </h2>
    <h2>
        this text can be selected
    </h2>
    <h2 class='noselect'>
        this text cannot be selected
    </h2>
</body>
</html>

None of the above solutions worked for me-though I'm sure that was due to user error. 上述解决方案均不适用于我-尽管我确定这是由于用户错误造成的。 We have Jquery already running, and turns out they are already set up for this. 我们已经在运行Jquery,事实证明已经为此设置了它们。 We used this code in the head section, and it works great! 我们在开头部分使用了此代码,效果很好!

<script language="javascript" type="text/javascript">
$(document).ready(function() {
//alert($(".unselectable").length)
var returnFalse = function () { return false; },
els = $(".yourClassNameHere"),
n = els.length;
while (n--) {
els[n].onselectstart = returnFalse;
}
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM