我在设置要在 $(document).ready
时执行的 JavaScript 函数时遇到问题, 我得到 p>
Uncaught TypeError: Property '$' of object [object Object] is not a function
当我尝试使用 document.onload
时我明白了
Uncaught TypeError: Cannot read property 'src' of null
当对 body.onload
做同样的事情时,它只是不识别 body 。
JavaScript 代码在一个文件中,在 wordpress 插件中注册
wp_enqueue_script( 'dencoder', plugins_url( 'decoder.js' , __FILE__ ), array('jquery'));
如您所见,我已经将 jQuery 设置为依赖项,但我没有让它工作。 这是我的 javascript 代码
function decode2(){
var element = document.evaluate( '//*[@id="mep_0"]/div/div[1]/video' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
var theurl_e = element.src.replace(document.URL,'');
var theurl = rot13(atob(element.src.replace(document.URL,'')));
element.src = theurl;
var element2 = document.evaluate( '//*[@id="mep_0"]/div/div[1]/video/source' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
element2.src = theurl;
}
function rot13(s)
{
return (s ? s : this).split('').map(function(_)
{
if (!_.match(/[A-za-z]/)) return _;
c = Math.floor(_.charCodeAt(0) / 97);
k = (_.toLowerCase().charCodeAt(0) - 83) % 26 || 26;
return String.fromCharCode(k + ((c == 0) ? 64 : 96));
}).join('');
}
最佳答案
使用 jQuery()
代替 $()
WordPress 使用 jQuery()
来避免与其他库发生冲突。
关于Javascript $(document).ready 错误与 Wordpress 中的 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17442868/