[英].css() on DOM element not activating
Been trying for hours, but can't get this to work. 尝试了几个小时,但无法正常工作。
If I go direct 如果我直接去
$(.button).click(function(event){
var graham = window.document.childNodes[1].childNodes[2].childNodes[1].childNodes[9];
$(graham).css('color', 'red');
)};
But instead of just typing out the DOM node directly, if I cycle it through a loop like: 但是,如果我通过类似以下的循环来循环它,而不是直接输入DOM节点:
$(.button).click(function(event) { js = (js.slice(0, -1)).split(","); for(x = 0; x< js.length; x++) { js[x] = "childNodes[" + js[x] + "]"; } js = "window.document." + js.join("."); $(js).css('color', 'red');
)};
)};
Nothing happens onclick. onclick没有任何反应。 Is there something wrong with using 'window.document' as a string and appending it to the childNodes data?
使用“ window.document”作为字符串并将其附加到childNodes数据有什么问题吗? I thought this would be straight forward, but can't get it to work!..
我以为这很简单,但无法正常工作!
The problem is that jQuery sees js
as a string not an object : 问题是jQuery将
js
视为字符串而不是对象:
This changes the Stackoverflow website <body>
to green : 这会将Stackoverflow网站
<body>
更改为绿色:
$(window.document.childNodes[1].childNodes[2]).css('background', 'green');
This doesn't : 这不是:
$('window.document.childNodes[1].childNodes[2]').css('background', 'green');
However this does : 但是这样做:
$(eval('window.document.childNodes[1].childNodes[2]')).css('background', 'green');
Try it in your console. 在您的控制台中尝试。
You need to do : 您需要做:
$(eval(js)).css('color', 'red');
If you are already using jQuery it is not necessary to search nodes with the native NodeList object via childNodes. 如果您已经在使用jQuery,则无需通过childNodes使用本机NodeList对象搜索节点。 Just rely on a jQuery selector.
只需依靠jQuery选择器即可。
If jQuery is not used, you can rely on native DOM methods such as querySelector, getElementById, getElementsByTagName, getElementsByClassName, etc. 如果不使用jQuery,则可以依赖于本机DOM方法,例如querySelector,getElementById,getElementsByTagName,getElementsByClassName等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.