2013-03-01 26 views
0

嗨,我试图让警报方法显示值C8,但它只是说未定义。jquery alert undefined

我是新来的HTML和jQuery;这很简单,但我不能看到它。

<!DOCTYPE html> 
<html> 
    <head> 
    <script type="text/javascript" src="jquery/jquery-1.7.1.js"></script> 
    <script> 
     $(document).ready(function(){ 
     $("button").click(function(){ 
     //$("#div1").load("hs.php"); 
     //$("#div1").load("http://127.0.0.1:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=A1"); 
     $("#div1").load("http://127.0.0.1:81/tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=c8"); 
     alert($(this).prev().attr(".class")); 
     }); 
     }); 
    </script> 
    </head> 
    <body> 
    <div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div> 
    <span class='c8'><button>Get External Content</button></span> 
    </body> 
</html> 
+0

你可以把它放在jsfiddle中吗?另外,我认为它应该是attr(“class”),没有“。”。 (期间) – 2013-03-01 22:22:18

+1

@Ohgodwhy最初我认为这是问题,但它只是可怕的缩进。这里没有'.load()'回调。 – 2013-03-01 22:23:39

+0

@马特球好抓。我会看到自己。 – Ohgodwhy 2013-03-01 22:24:13

回答

0

你不需要的属性名前点:

alert($(this).prev().attr("class")); 
0

虽然jQuery中的.引入了类选择,在这种情况下,你只是想叫“下课”的属性。

取出..class

alert($(this).prev().attr("class")); 
0

您应该使用.attr("class")代替(不.

1

因为上一个()仅供sibblings。你的按钮是该div的子项。

你应该去:

alert($(this).parent().attr("class")); 
+0

感谢用父母替换prev并删除了点 – user2125298 2013-03-01 22:31:53

1

按钮没有兄弟姐妹.prev()用来选择上一个兄弟,你要定位的元素,因此其母公司使用.parent()选择它。此外,只有当它是选择器的一部分时,才会使用点(.)将类添加到该类中。

alert($(this).parent().attr("class"));