ASP.NET Web表单中的Javascript问题

ASP.NET Web表单中的Javascript问题,javascript,asp.net,Javascript,Asp.net,我在UL里面有一个HREF列表。当用户单击某个链接时,我需要对该href应用特定的CSS样式(即,将其设置为“选中”,并将所有其他样式设置为“未选中”)。下面给出了我使用的脚本。但它不起作用。知道哪里出了问题吗 谢谢 抗心磷脂 抗凝血酶 document.onclick=函数(evt){ var el=window.event?event.src元素:evt.target; var aText=“”; 如果(el&&el.className==“未选中”){ el.className=“已

我在UL里面有一个HREF列表。当用户单击某个链接时,我需要对该href应用特定的CSS样式(即,将其设置为“选中”,并将所有其他样式设置为“未选中”)。下面给出了我使用的脚本。但它不起作用。知道哪里出了问题吗

谢谢

    抗心磷脂 抗凝血酶

document.onclick=函数(evt){
var el=window.event?event.src元素:evt.target;
var aText=“”;
如果(el&&el.className==“未选中”){
el.className=“已选定”;
aText=el.innerText;
对于(变量i=0;i
虽然我希望看到HTML,但我强烈建议您使用Javascript框架,这将大大减少代码的使用

只需将脚本
src
添加到文档中即可

并且只使用这个代码

<script type="text/javascript">

    // well all the DOM is written in the page and all elements are available do:
    $(document).ready( function() {

        // for each <a> tag bind the event click and now do:     
        $("a").click(function() {

            // 1st. remove all selected
            $("a.selected").removeClass("selected");

            // 2nd. assign selected to this clicked element only
            $(this).addClass("selected");

            // 3rd. let's return true so the <a> tag can jump to the href
            return true;    
        });
    });

</script>

//所有DOM都写在页面中,所有元素都可用,请执行以下操作:
$(文档).ready(函数(){
//对于绑定事件的每个标记,单击并立即执行:
$(“a”)。单击(函数(){
//1.删除所有选定的
$(“a.selected”).removeClass(“selected”);
//2.仅将所选内容分配给此单击的元素
$(此).addClass(“选定”);
//3.让我们返回true,以便标记可以跳转到href
返回true;
});
});

无需取消选择
类,这应该是默认的CSS。

尽管我希望看到HTML,但我强烈建议您使用Javascript框架,这将大大减少代码的使用

只需将脚本
src
添加到文档中即可

并且只使用这个代码

<script type="text/javascript">

    // well all the DOM is written in the page and all elements are available do:
    $(document).ready( function() {

        // for each <a> tag bind the event click and now do:     
        $("a").click(function() {

            // 1st. remove all selected
            $("a.selected").removeClass("selected");

            // 2nd. assign selected to this clicked element only
            $(this).addClass("selected");

            // 3rd. let's return true so the <a> tag can jump to the href
            return true;    
        });
    });

</script>

//所有DOM都写在页面中,所有元素都可用,请执行以下操作:
$(文档).ready(函数(){
//对于绑定事件的每个标记,单击并立即执行:
$(“a”)。单击(函数(){
//1.删除所有选定的
$(“a.selected”).removeClass(“selected”);
//2.仅将所选内容分配给此单击的元素
$(此).addClass(“选定”);
//3.让我们返回true,以便标记可以跳转到href
返回true;
});
});

没有必要取消选中
类,这应该是默认的CSS。

您是否想过使用像jQuery这样的Javascript框架,这会使它变得很简单?您所说的“它不工作”是什么意思?作为一名程序员,你能更具体一点吗?电流输出是多少?遇到错误了吗?你有没有想过使用像jQuery这样的Javascript框架,这会使它变得很简单?你说的“它不工作”是什么意思?作为一名程序员,你能更具体一点吗?电流输出是多少?遇到任何错误吗?嗨。。找到了罪犯。。实际上,此代码驻留在(ASP.NET)母版页中。当用户单击一个链接时,目标页面(对于该页面也是相同的母版页)将加载(与母版页一起),因此所有脚本功能都将丢失。必须找到解决办法。谢谢你的回复!你好找到了罪犯。。实际上,此代码驻留在(ASP.NET)母版页中。当用户单击一个链接时,目标页面(对于该页面也是相同的母版页)将加载(与母版页一起),因此所有脚本功能都将丢失。必须找到解决办法。谢谢你的回复!