开发者

Jquery - Change an class with "=" in it

I have an website with URL`s like example.com/index.php?p=home An this script

$(document).ready(function() {

    var pathname = jQuery.url.attr("query");
    $('.tab_container').append(' .'+ pathname +' ');

    //Default Action
    $(".tab_content").hide(); //Hide all content
    $(' .'+ pathname +' ').show();

    //On Click Event
    $("#sidebar-menu a").click(function() {
        $("#sidebar-menu a").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });

});

And and h开发者_JAVA技巧tml width divs that contain the pages class like

<div id="tab1" class="tab_content p=home">

Can I have an script that will remove the = sign from the path name variable and from the classes of div`s?

Thank you!


Because you are producing invalid HTML, it is not guaranteed how the browser will expose the classes of elements to the browser. You could try:

$('div[class*="'+pathname+'"]').attr('class',function(i,className){
  return className.replace(/=/g,'');
});

This uses the "Attribute Contains" selector to find a div with this string in it, and the attr setter method to change the value.


pathname = pathname.replace(/=/g,'');
classes = document.getElementById('tab1').className;
$('#tab1').removeClass().addClass(classes.replace(/=/g,'');

Since my answer was accepted I better make it more like Phrogz's answer, which was voted up. The attr set function is nice:

$('div[class*="'+pathname+'"]').attr('class',function(i,className){  
  return className.replace(/=/g,'');  
});
pathname = pathname.replace(/=/g,'');


Since, I see that the URL is a PHP page, will not it be easier to use PHP to store the variable ($_GET['p']) and then run a str_replace on it (or strtr), and pass this variable to jQuery?

(Its just a suggestion, since the page is a PHP page. I am sorry, if this doesnt apply to you)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜