简体   繁体   中英

Can i access html element Id from any from anyfiles?

I am a beginner to Java-script.

i have a concern

on mainPage.html 
<div id="showDialog" style="display:none"> </div>

on mainPage.js

 $('#ShowDialog').load("../xxxx.html", function (content) {
xxxxxxxxxxx
     });

It is easy understand that if you want to use the id element you just call #element

So, my question is here:

if you are in Setup.js (setup.html has no id attribute like #ShowDialog)

can you still call (So, it will do something on mainPage.html ??

 $('#ShowDialog').load("../xxxx.html", function (content) {
xxxxxxxxxxx
     });

If so, is the class also do the same job? i know id is unique and class is common. So, if one file class can be accessed from other file?

like

$('.ShowDialog').load("../xxxx.html", function (content) { xxxxxxxxxxx });

By the way, why we call class using $(.'ShowDialog')rather than just call name like $('ShowDialog')

for class="ShowDialog"

Could you please explain what is it that you are trying to do?

As I understand from your code, you are trying to load content from an external HTML to other HTML with the load function in JQuery and place it inside the div with id="showDialog"

$('#ShowDialog').load("../xxxx.html", function (content) {...}

Is this what you are trying to do? or are you trying to run some JavaScript from other file?

What do you mean by:

setup.html has no id attribute like #ShowDialog

does setup.html does not have any div with that id?

and by:

can you still call (So, it will do something on mainPage.html ??

call what? the loader.

about:

By the way, why we call class using $(.'ShowDialog')rather than just call name like $('ShowDialog')

The preceding dot (.) is because classes on jQuery selectors are specified with a preceding dot (.), and id's with a hash sign (#), think of it, like Css selectors, but in jQuery.

Please explain exactly what are you trying to do so we can help you better :)

If you want to reference an html Element from an external Javascript file it could be as follows:

myHtml.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <!-- First Import Jquery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
    <!-- Then the module you wish to work with-->
    <script src="./ReferencebyId.js"></script>


    <title>referencing html Element by Id</title>
</head>
<body>
    <!-- the Id you will reference on the module -->
    <p id="myId">Change this text</p>
    <button onclick="changeInnerHtml()">Change paragraph text</button>
</body>
</html>

ReferencebyId.js

function changeInnerHtml(){
  //This is how you would reference the Id, with '#' character
  $("#myId").text("changed from external js file referencing Id");
}

Remember that if you wish to use the class to reference the Html Element, you have to make sure only that element has that class, or if you wish to affect more than one element you can put them the same class, and reference them as $(".nameOfClass")

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