简体   繁体   中英

How to manipulate imported HTML file using external jQuery file?

I imported a html file using jquery like below,

$(".IncludeInThisDiv").load("ToBeImported.html");

My ToBeImported.html file is like below,

在此处输入图片说明

Now I want to change the background color of testDiv which is imported in lets say main.html file. To do so I added a js file called main.js in the project and included it in the main.html file by proper manner. And I have been written the proper jquery code to change the background color of testDiv in main .js file, But that is not working at all.

I think there is some issue with that.

Note: I can manipulate the color from browser console using same jQuery code.

Probably you are changing div's css before it loads completely.

Call load function with complete handler like this:

$(".IncludeInThisDiv").load("ToBeImported.html", function() {
  $(".testDiv").css("background-color","red");
});

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