注册 登录
编程论坛 JavaScript论坛

新人菜鸟想求解一个js问题

relten 发布于 2022-07-17 08:58, 337 次点击
根据url地址参数来实现分页当前状态样式,
url地址:http://localhost:8014/products.asp?page=1&sortID=
页面代码:
程序代码:
<i  id="pronumber">
                <%for i = 1 to n%>
                    <a href="<%=filename%>?page=<%=i%>&amp;<%=url%>"><span id="spanhover"><%=i%></span></a>
                <%next%>
            </i>

js代码:
程序代码:
<script type="text/javascript">

        function getQueryVariable(variable){
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                   var pair = vars[i].split("=");
                   if(pair[0] == variable){
                       return pair[1];
                   }
            }
            return(false);
        }            
        var page = getQueryVariable("page"); //提取url链接page后面的数值,赋予page
        
        
        
        
        var spanhover = document.getElementById("spanhover");
        var spans = document.querySelectorAll("#pronumber span");
        function myFun(){
            for( var j=0 ; j<spans.length ; j++ ){
                var spanid = spans[j].innerText;

                if( spanid == page ){
                    spanhover.style.background = "red";
                    spanhover.style.color = "#fff";
                };
            };
        };
        myFun();
        
现在的问题是点击第2页时,样式还是作用在第1个。
这里要怎么修改,求告知。真诚感谢!!!        
1 回复
#2
apull2022-07-19 11:56
if( spanid == page ){                  
    spans[j].style.background = "red";
    spans[j].style.color = "#fff";
};

这个直接在asp文件中设置比较方便
增加个样式
<style>
    .nowa{
        background:red;
        color:#fff;
    }
</style>

获取页码
<%
page=int(request("page"))
%>

根据页码添加样式
<i  id="pronumber">
    <%for i = 1 to n%>
        <a href="<%=filename%>?page=<%=i%>&amp;<%=url%>"><span id="spanhover"  <% if page=i then Response.Write "class='nowa'" %>><%=i%></span></a>
    <%next%>
</i>

[此贴子已经被作者于2022-7-19 12:15编辑过]

1