我有这段代码
<html>
<head>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="jquery-ui-1.8.20.custom.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function MyFunc(obj,method){
// obj.hide("slow"); // This part is working fine i.e. it is hiding the paragraph
obj + "." + method + "(\"slow\")"; // This is not working
$(function(){
$("#pid1").click(function(){
MyFunc($("#pid1"),"hide");
});
});
</script
</head>
<body>
<p id = "pid1">Test Paragraph</p>
</body>
</html>
即我想将参数传递给jquery hide函数并形成正确的字符串来调用效果,但它不像上面的代码那样工作。我错过了什么吗?
答案 0 :(得分:1)
您可以使用方括号来访问具有字符串的对象的属性:
function MyFunc(obj, method) {
obj[method]("slow");
}
另请注意,您错过了MyFunc
的结束大括号。
您目前正在做的只是通过将一串字符串与一个对象连接来生成一个字符串。它不会抛出错误,但它不会做任何事情。