[英]How to make jquery like function to work?
I can't figure out what is wrong with the following code. 我无法弄清楚以下代码有什么问题。 I am trying to achieve jquery like functionality but following code does not work for me.
我正在尝试实现类似jquery的功能,但是以下代码对我不起作用。 I can see that console.log(dome) prints all the paragraphs in dome array but when i call this array it does not show up any results.
我可以看到console.log(dome)打印出圆顶阵列中的所有段落,但是当我调用该阵列时,它不会显示任何结果。 Can you please help?
你能帮忙吗?
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing</title>
<script>
function uquery(domElement){
console.log("In uquery function");
var uqObject={} // end object
uqObject.css=function(property, value){
var dome = document.getElementsByTagName(domElement);
console.log(dome);
console.log(property, value);
console.log(dome.length);
for(i=0; i<dome.length; i++ ){
console.log("loop count"+i);
if(property=="color")
{
dome[i].style.color=value;
}
}//end loop
}
return uqObject;
}// end uquery
uquery("p").css("color", "red");
//var obj=uquery("p");
//obj.css("color", "red");
</script>
</head>
<body>
<p>Testing Jquery like style</p>
<p>See if it works</p>
<p>Third Paragraph</p>
</body>
</html>
You need to call the function after the elements are added to the dom, like on the window.onload event 您需要在元素添加到dom之后调用函数,例如window.onload事件
window.onload = function () {
uquery("p").css("color", "red");
}
Also have a look at the bracket notation to access the dynamic properties 也可以看看括号符号来访问动态属性
dome[i].style[property] = value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.