简体   繁体   中英

Re-apply jQuery select box styling when new select box is added to page

My jQuery select box styling works on the initial page load, but when content with a select box is dynamically loaded into the page, the styling isn't applied to it.

What's the best way to make sure the new select box will be styled as soon as it is loaded onto the page?

jQuery(document).ready(function() {
    jQuery.jStyling.createSelect(jQuery('select'));
});

Try using CSS instead of jQuery

select {
    //Styling
}

Or by class or id if needed.

.classOfSelect {
    //Styling
}

I've had this issue when dynamically making tables with alternating band colors, this is how I did it:

Working Fiddle

HTML:

<input id="add" type="button" value="Add Box"></input>
<div class="styled">BOX</div>

JavaScript:

function addStyle() {
    $(".styled").css({
        "width": "100px",
        "height": "100px",
        "background": "#ff0000"
    });
};

$(document).on("click", "#add", function () {
    var myBox = $("<div>");
    myBox.addClass("styled");
    myBox.text("BOX");
    myBox.appendTo($("body"));
    addStyle();
});

addStyle();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM