在这个简单的脚本中我得到错误"obj.parentNode.getElementById不是一个函数",我不知道,出了什么问题.
<script type="text/javascript">
function dosomething (obj) {
sibling=obj.parentNode.getElementById("2");
alert(sibling.getAttribute("attr"));
}
</script>
<body>
<div>
<a id="1" onclick="dosomething(this)">1</a>
<a id="2" attr="some attribute">2</a>
</div>
</body>
Run Code Online (Sandbox Code Playgroud) 我有一类这样的课
Class Car {
private $color;
public function __construct($color){
$this->color=$color;
}
public function get_color(){
return $this->$color;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建它的一些实例:
$blue_car = new car('blue');
$green_car = new car('green');
etc.
Run Code Online (Sandbox Code Playgroud)
现在我需要根据实例的名称动态调用方法get_color()
$instance_name='green_car';
Run Code Online (Sandbox Code Playgroud)
有什么办法吗?