很难弄清楚你在问什么。 听起来好像你在问如何通过打开不同的对话框来响应点击不同类的不同“属性”链接。这将是微不足道的:只需使用 bind (click)、delegate 或 live 将点击事件挂在相关的“属性”链接上:
$("a.type1").click(function() {
// Open the dialog for type1
// ...
// Prevent the default action of the link
return false;
});
$("a.type2").click(function() {
// Open the dialog for type2
// ...
// Prevent the default action of the link
});
(同样,您可以使用delegate 或live,而不是将事件绑定到元素本身,如果这是动态的话。)
或者,如果您想对处理程序中的所有类型和分支使用通用处理程序,您可以这样做:
$("a.type1, a.type2, a.type3").click(function() {
// ...code all of them have in common...
// ...
// Branch on what class(es) the link has and open the relevant dialog
// ...
// ...more code all of them have in common...
// ...
// Prevent the default action of the link
return false;
});
您可以使用多个类(因此每个链接都有“props”和“type1”或“type2”等)
如果你的意思是链接不会实际上有不同的类型,但你想在它们所在的同一个容器中分支什么,你可以使用closest你想要的容器,然后find 和/或children 找出容器中的内容:
$("a.props").click(function() {
var container = $(this).closest('div'); // If the container is a div
if (container.find("textarea")[0]) {
// It has at least one text area, show dialog...
}
else if (container.find("input[type=text]")[0]) {
// It has at least one text input, show dialog...
}
else if (container.find("input[type=button]")[0]) {
// It has at least one `input`-style button, show dialog...
}
// else etc.
// Prevent the default action of the link
return false;
});