简体   繁体   English

如何将此 jquery 代码更改为 javascript?

[英]How to change this jquery code to javascript?

I have been reading and studying about this library to be able to change the code of this codebox from jquery to javascript ( https://codepen.io/mrWilson123/pen/OJMVwzd?editors=1010 ) but I got stuck in this part.我一直在阅读和研究这个库,以便能够将这个代码框的代码从 jquery 更改为 javascript ( https://codepen.io/mrWilson123/pen/OJMVwzd?editors=1010 ),但我被困在这部分。

$(".slide-captions").html(function () {
        return (
          "<h2 class='current-title'>" +
          currentTitle +
          "</h2>" +
          "<h3 class='current-subtitle'>" +
          currentSubtitle +
          "</h3>"
        );
      });

I understand that it's getting the div element and putting inner html code into it so I tried this but it didn't work.我知道它正在获取 div 元素并将内部 html 代码放入其中,所以我尝试了这个,但它没有用。

document.querySelector(".slide-captions").innerHTML(
          "<h2 class='current-title'>" +
            currentTitle +
            "</h2>" +
            "<h3 class='current-subtitle'>" +
            currentSubtitle +
            "</h3>");

What am I doing wrong if someone could help me, thanks.如果有人可以帮助我,我做错了什么,谢谢。

Whenever appending to the DOM , there is a string-oriented approach using parse-able strings of HTML and properties and methods like:每当附加到DOM时,都有一种面向字符串的方法,它使用可解析的 HTML 字符串以及属性和方法,例如:

  • .innerHTML
  • .insertAdjacentHTML()

There is also a construction-oriented approach using DOM Nodes and properties and methods like:还有一种使用 DOM 节点和属性和方法的面向构造的方法,例如:

  • .insertBefore() / .before() .insertBefore( .insertBefore() / .before()
  • .appendChild / .append() .appendChild / .append()
  • .insertAdjacentElement()
  • .classList

Whichever approach you normally use to build DOM, it's useful to be aware of the other approach:无论您通常使用哪种方法来构建 DOM,了解另一种方法很有用:

String-oriented Approach面向字符串的方法

let slideCaptions = document.querySelector('.slide-captions');

let myHTML = '';
myHTML += '<h2 class="current-title">' + currentTitle + '</h2>';
myHTML += '<h3 class='current-subtitle'>' + currentSubtitle + '</h3>';

slideCaptions.innerHTML = myHTML;

Construction-oriented Approach建设导向的方法

let slideCaptions = document.querySelector('.slide-captions');

let titleElement = document.createElement('h2');
titleElement.classList.add('current-title');
titleElement.textContent = currentTitle;
slideCaptions.appendChild(titleElement);

let subtitleElement = document.createElement('h3');
subtitleElement.classList.add('current-subtitle');
subtitleElement.textContent = currentSubtitle;
slideCaptions.appendChild(subtitleElement);

Try this :尝试这个 :

document.querySelector('.slide-captions').innerHTML=`
        <h2 class='current-title'>" +
        currentTitle +
        "</h2>" +
        "<h3 class='current-subtitle'>" +
        currentSubtitle +
        "</h3>`;

if there any error use this instead如果有任何错误,请改用它

document.querySelectorAll('.slide-captions')[0] // .innerHTML ....

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

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