How to check if some element has attribute “name” starts with something, not “value”

后端 未结 3 965
刺人心
刺人心 2021-01-26 08:39

I\'m going to use various data attributes names start with for example \"data-mo-\".

Assume I have these elements:



        
3条回答
  •  醉酒成梦
    2021-01-26 09:04

    If I get it right you want to check for data-mo so I have tried something to make this work;

    function getData(index){
    	if(index[0].indexOf("mo") !== -1)
        	return true
            
        return false
    }
    
    $.each($("span"), function(i,index){
    	var objectKey = Object.keys($(this).data());
    	if(getData(objectKey)){
        	$(this).addClass("valid")
        } else {
        	$(this).addClass("not-valid")
        }
        	
    })
    .valid {
        color: green
    }
    .not-valid {
        font-weight: bold;
        color: red;
        text-decoration: line-through
    }
    
    Title 1
        Title 2
        Title 3
        Title 4
        Title 5

提交回复
热议问题