简体   繁体   English

a.js创建HTML按钮calls.js function成功,但是在html页面抹掉all.css

[英]A .js created HTML button calls .js function successfully, but wipes out all .css on the html page

I'm new to javascript and self teaching - running into an issue I can't seem to find answers to or related examples.我是 javascript 和自学的新手 - 遇到一个我似乎无法找到答案或相关示例的问题。

Issue: HTML button calls =.js function successfully, but wipes out all.css on page问题:HTML 按钮调用 =.js function 成功,但擦除页面上的所有 .css

Context:语境:

  • I have an html that uses.css (works)我有一个 html,它使用 css(有效)
  • html loads a.js (works) html 加载 a.js(有效)
  • .js makes a button on html (works) .js 在 html 上创建一个按钮(有效)
  • button calls.js function (works, but wipes out all the.css' formatting)按钮 calls.js function(有效,但清除了所有的 .css 格式)

Any thoughts?有什么想法吗? Thank you.谢谢你。

var button = document.createElement("button");
button.innerHTML = "Press Me";

var body = document.getElementsByTagName("body")[0];

body.appendChild(button);

button.addEventListener("click", function() {
    Main();
});

Here's the entire.js:这是 entire.js:

  const myHeading = document.querySelector('h1');
  myHeading.fontFamily = "courier";
  myHeading.textContent = "Welcome!";

Main:
  Main();
  
Functions:
  function Main() {
    DisplayStuff();
    DisplayUI();
  }
  function DisplayStuff() {
    WriteMe ("Hello!");
    }
  

  function DisplayUI(){
    //Buttons
    var button = document.createElement("button");
    button.innerHTML = "Press Me";

    var body = document.getElementsByTagName("body")[0];
    body.appendChild(button);

    button.addEventListener ("click", function() {
       Main();
    });
  }

  function WriteMe(x) { var R = "<br>"; document.write(x + R);} ```

The document.write() was in fact the culprit clearing out all the.css on the page. document.write() 实际上是清除页面上所有 the.css 的罪魁祸首。 Instead I created something on the page to send text to.相反,我在页面上创建了一些东西来发送文本。 In this case, a table's.innerHTML and the.css remained intact.在这种情况下,表的.innerHTML 和.css 保持不变。

myTable.rows[r].cells.item(c).innerHTML = h1.textContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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