H5W3
当前位置:H5W3 > 其他技术问题 > 正文

【Web前端问题】前端页面找不到className

问题描述

图片描述 浏览器报错,(找不到className,是这个意思吗?),应该怎么解决呢。
还有一个问题,传入的值为元素,元素是个什么定义呢?

相关代码

这是前端页面

                <div>
                    <ul class="nav navbar-nav">
                        <li id="featureLi"><a href="../BranchSimpleInfo.html" onclick="setCookieFeature()" id="featureA">特性分支</a></li>
                        <li id="integrationLi"><a href="../BranchSimpleInfo.html" onclick="setCookieIntegration()" id="integrationA">集成分支</a></li>
                        <li id="publishedLi"><a href="../BranchSimpleInfo.html" onclick="setCookiePublished()" id="publishedA">历史版本</a></li>
                    </ul>
                </div>

这是common.js

<script>
function hasClass( elementone,cName ){
    if (!elementone||elementone.className==null){
        return false;
    }
    return !!elementsone.className.match( new RegExp( "(\\s|^)" + cName + "(\\s|$)") );
}

function addClass( elementOne,cName ){
    if(!hasClass( elementOne,cName ) ){  
        elementOne.className += " " + cName; //这里是common.js第96行
    };
}

function removeClass( elementOne,cName ){
    if( hasClass( elementOne,cName ) ){
        elementOne.className = elementOne.className.replace( new RegExp( "(\\s|^)" + cName + "(\\s|$)" )," " ); // replace方法是替换
    };
}
</script>

这是方法的调用:

function onloadTable(){
    var BranchTypeID = getCookie("BranchTypeID");
    var featureLi = document.getElementById("featureLi");
    var integrationLi = document.getElementById("integrationLi");
    var publishedLi = document.getElementById("publishedLi");
    var Feature = document.getElementById("Feature");
    var Integration = document.getElementById("Integration");
    var Published = document.getElementById("Published");
    if (BranchTypeID == 1) {
        //为这个类移除属性
        addClass(featureLi, "active");
        removeClass(integrationLi, "active");
        removeClass(publishedLi, "active");
        $("#featureA").attr("aria-expanded", "true");
        $("#integrationA").attr("aria-expanded", "false");
        $("#publishedA").attr("aria-expanded", "false");
        addClass(Feature, "active");
        removeClass(Integration, "active");
        removeClass(Published, "active");
    }   
    if (BranchTypeID == 2) {
        //为这个类移除属性
        removeClass(featureLi, "active");
        addClass(integrationLi, "active");
        removeClass(publishedLi, "active");
        removeClass(Feature, "active");
        addClass(Integration, "active");
        removeClass(Published, "active");
        $("#featureA").attr("aria-expanded", "false");
        $("#integrationA").attr("aria-expanded", "true");
        $("#publishedA").attr("aria-expanded", "false");
    }
    if (BranchTypeID == 3) {
        //为这个类移除属性
        removeClass(featureLi, "active");
        removeClass(integrationLi, "active");
        addClass(publishedLi, "active");
        $("#featureA").attr("aria-expanded", "false");
        $("#integrationA").attr("aria-expanded", "false");
        $("#publishedA").attr("aria-expanded", "true");
        removeClass(Feature, "active");
        removeClass(Integration, "active");
        addClass(Published, "active");

    }
    
    setInterval("checkPipelineStatus()", 10000);
}

回答:

看了下代码,你传入addClass,removeClass,hasClass的第一个参数应该element的但是你传入的却是element的className也就是一个字符串

回答:

var applyMergeLi = document.write(document.getElementById('applyMergeLi').className);
var applyPublishLi = document.getElementById("applyPublishLi").className;
var mergeReviewLogLi = document.getElementById("mergeReviewLogLi").className;
var publishReviewLogLi = document.getElementById("publishReviewLogLi").className;
var ApplyMerge = document.getElementById("ApplyMerge").className;
var ApplyPublish = document.getElementById("ApplyPublish").className;
var MergeReviewLog = document.getElementById("MergeReviewLog").className;
var PublishReviewLog = document.getElementById("PublishReviewLog").className; 

每行去掉.className
第一个再去掉document.write

回答:

function addClass( elementOne,cName )
{

if( !hasClass( elementOne,cName ) ){   


    elementOne.className += " " + cName;
};
}

这一部分,elementOne.className的elementOne必须为元素,才会有className。
而addClass(applyMergeLi,”active”);的
var applyMergeLi = document.write(document.getElementById(‘applyMergeLi’).className);
var applyPublishLi = document.getElementById(“applyPublishLi”).className;
var mergeReviewLogLi = document.getElementById(“mergeReviewLogLi”).className;
var publishReviewLogLi = document.getElementById(“publishReviewLogLi”).className;
var ApplyMerge = document.getElementById(“ApplyMerge”).className;
var ApplyPublish = document.getElementById(“ApplyPublish”).className;
var MergeReviewLog = document.getElementById(“MergeReviewLog”).className;
var PublishReviewLog = document.getElementById(“PublishReviewLog”).className;
这里所有的className必然重复了。所以每行去掉.className;第一个再去掉document.write。

回答:

没看到你讲报错是报啥错。。。。

回答:

把错误信息贴上来吧,

var Feature = document.getElementById("Feature");
var Integration = document.getElementById("Integration");
var Published = document.getElementById("Published");
//这三个ID确定存在吗

还有jQuery自带addClass/removeClass 这些API

本文地址:H5W3 » 【Web前端问题】前端页面找不到className

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址