最好用一个例子来解释:http://jsfiddle.net/F4H46/
总结:
(a)通过单击锚标记来触发JQuery脚本。
(b)期望的结果:在href id=?
之后获取字符并保存到变量
(c)$(this)[0] 确实包含正确的href
(d)使用.match(正则表达式)去除所需的字符会产生404错误。
为什么会出现404错误?如果相同的字符串是硬编码的,则.match(正则表达式)完美地工作。
答案 0 :(得分:1)
$("a").bind('click', function(e) {
e.preventDefault();
var xxx = $(this).attr('href');
alert(xxx);
var yyy = xxx.match(/\=(\d*)(\w*)/);
alert(yyy[0]);
alert(yyy[1]);
alert(yyy[2]);
});
答案 1 :(得分:0)
单击锚标记时,页面将自动更改页面的href,导致404导致页面不存在。单击锚标记后要做的第一件事是:e.preventDefault()
,阻止页面被重定向。
$("a").click(function(e){
e.preventDefault();
// rest of the code
});
答案 2 :(得分:0)
你好这个:
<script type="text/javascript">
$("a").click(function()
{ var xxx = $(this).attr('href');
alert(xxx);
var yyy = xxx.match(/\=(\d*)(\w*)/);
alert(yyy[0]);
alert(yyy[1]);
alert(yyy[2]);
});
</script>
这种有效,不同之处在于您需要使用attr('href');