【问题标题】:Change page and then change a Class更改页面,然后更改类
【发布时间】:2018-08-30 06:09:35
【问题描述】:

是否可以在单击跳转到该页面的链接后更改一个类(按 ID)? 我有第 01 页,当我单击第 01 页中的链接时,我希望它跳转到第 02 页,然后更改第 02 页中的类。 有可能吗?

【问题讨论】:

    标签: javascript css hyperlink onclick


    【解决方案1】:

    您可以将查询字符串添加到 page01 上的链接,然后在页面加载时由脚本 page02 解析,这将改变 page02 上的相关类/元素。

    【讨论】:

      【解决方案2】:

      你的问题不清楚。我假设您从第 1 页检索 id,在第 2 页通过 id 选择元素并更改其类。首先,您必须将 URL 参数添加到链接,并将其在第 01 页中的href 属性设置为https://yourlink.com?id=id-of-element-to-change。在页面 02 中,当窗口加载完页面后执行以下脚本:

      window.onload = function () {
        let url = new URL(document.location.href);
        let id = url.searchParams.get("id");
        document.getElementById(id).setAttribute("class", "your-class");
      };
      

      【讨论】:

        【解决方案3】:

        其实被删的帖子对我帮助很大:)

        我用这段代码来获取参数

                    function getParam( name ){
                    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
                    var regexS = "[\\?&]"+name+"=([^&#]*)";
                    var regex = new RegExp( regexS );
                    var results = regex.exec( window.location.href );
                    if( results == null )
                        return "";
                        else
                        return results[1];
                }
        

        然后删除该类并添加一个新的

                var idPassing = getParam( 'id' );
        
                if(idPassing == "Publicidade"){
                    functRemoveClassCurrent();
                    document.getElementById("idFilterPublicidade").className+="current";
                }
        

        谢谢大家!!!

        【讨论】:

          猜你喜欢
          • 2023-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-19
          相关资源
          最近更新 更多