# innerHTML 的文本 Javascript

# in text at innerHTML Javascript

我不知道如何获得“,我在使用 DOM.innerHTML 时需要在文本中使用它。

document.getElementById("info").innerHTML = "<div id='info01' class='border border-3 w-50 h-50 d-flex align-content-center shadow-lg p-3 mb-5 bg-body rounded flex-column'><h1 class='text-center'>Hent varer fra Traphous</h1><p class='text-center'><b>Kjøp noe:</b></p><button onclick='kjøp('kokain')' class='btn btn-primary w-50 align-self-center' type='button'>Kokain</button><br><button onclick='kjøp('hasj')' class='btn btn-primary w-50 align-self-center' type='button'>Hasj</button><br><button onclick='kjøp('amfetamin')' class='btn btn-primary w-50 align-self-center' type='button'>Amfetamin</button><br><button onclick='kjøp('benzo')' class='btn btn-primary w-50 align-self-center' type='button'>Benzo</button><br><button onclick='kjøp('molly')' class='btn btn-primary w-50 align-self-center' type='button'>Molly</button></div>"

我需要调用函数 onclick='endre("kjøp_narkotika") 我不能使用 onclick='endre('kjøp_narkotika') 因为函数不会调用。

    document.getElementById("info").innerHTML = `<div id='info01' class='border border-3 w-50 h-50 d-flex align-content-center shadow-lg p-3 mb-5 bg-body rounded flex-column'><h1 class='text-center'>Hent varer fra Traphous</h1><p class='text-center'><b>Kjøp noe:</b></p><button onclick='kjøp("kokain")' class='btn btn-primary w-50 align-self-center' type='button'>Kokain</button><br><button onclick='kjøp("hasj")' class='btn btn-primary w-50 align-self-center' type='button'>Hasj</button><br><button onclick='kjøp("amfetamin")' class='btn btn-primary w-50 align-self-center' type='button'>Amfetamin</button><br><button onclick='kjøp("benzo")' class='btn btn-primary w-50 align-self-center' type='button'>Benzo</button><br><button onclick='kjøp("molly")' class='btn btn-primary w-50 align-self-center' type='button'>Molly</button></div>`
    function kjøp(param){console.log(param)}
<div id="info"></div>

尝试一下,希望对您有所帮助。

ES6 支持反引号。

document.getElementById("info").innerHTML = `<div id='info01' class='border border-3 w-50 h-50 d-flex align-content-center shadow-lg p-3 mb-5 bg-body rounded flex-column'><h1 class='text-center'>Hent varer fra Traphous</h1><p class='text-center'><b>Kjøp noe:</b></p><button onclick='kjøp("kokain")' class='btn btn-primary w-50 align-self-center' type='button'>Kokain</button><br><button onclick='kjøp("hasj")' class='btn btn-primary w-50 align-self-center' type='button'>Hasj</button><br><button onclick='kjøp("amfetamin")' class='btn btn-primary w-50 align-self-center' type='button'>Amfetamin</button><br><button onclick='kjøp("benzo")' class='btn btn-primary w-50 align-self-center' type='button'>Benzo</button><br><button onclick='kjøp("molly")' class='btn btn-primary w-50 align-self-center' type='button'>Molly</button></div>`

方法 2:连接字符串

document.getElementById("info").innerHTML = "<div id='info01' class='border border-3 w-50 h-50 d-flex align-content-center shadow-lg p-3 mb-5 bg-body rounded flex-column'><h1 class='text-center'>Hent varer fra Traphous</h1><p class='text-center'><b>Kjøp noe:</b></p><button onclick='kjøp("+'"kokain"'+")' class='btn btn-primary w-50 align-self-center' type='button'>Kokain</button><br><button onclick='kjøp("+'"hasj"'+")' class='btn btn-primary w-50 align-self-center' type='button'>Hasj</button><br><button onclick='kjøp("+'"amfetamin"'+")' class='btn btn-primary w-50 align-self-center' type='button'>Amfetamin</button><br><button onclick='kjøp("+'"benzo"'+")' class='btn btn-primary w-50 align-self-center' type='button'>Benzo</button><br><button onclick='kjøp("+'"molly"'+")' class='btn btn-primary w-50 align-self-center' type='button'>Molly</button></div>`

您可以使用反引号 (`) 代替 (")

document.getElementById("info").innerHTML = `<div id='info01' class='border border-3 w-50 h-50 d-flex align-content-center shadow-lg p-3 mb-5 bg-body rounded flex-column'><h1 class='text-center'>Hent varer fra Traphous</h1><p class='text-center'><b>Kjøp noe:</b></p><button onclick="kjøp('kokain')" class='btn btn-primary w-50 align-self-center' type='button'>Kokain</button><br><button onclick='kjøp('hasj')' class='btn btn-primary w-50 align-self-center' type='button'>Hasj</button><br><button onclick='kjøp('amfetamin')' class='btn btn-primary w-50 align-self-center' type='button'>Amfetamin</button><br><button onclick='kjøp('benzo')' class='btn btn-primary w-50 align-self-center' type='button'>Benzo</button><br><button onclick='kjøp('molly')' class='btn btn-primary w-50 align-self-center' type='button'>Molly</button></div>`

在上面的示例中,我已将单引号更改为双引号以便单击按钮。

onclick="kjøp('kokain')"

您可以在此处查看示例: https://jsfiddle.net/jubish/w2ospk15/16/

点击按钮 Kokain 查看结果。

Method 2

document.getElementById("info").innerHTML = "<div id='info01' class='border border-3 w-50 h-50 d-flex align-content-center shadow-lg p-3 mb-5 bg-body rounded flex-column'><h1 class='text-center'>Hent varer fra Traphous</h1><p class='text-center'><b>Kjøp noe:</b></p><button onclick='kjøp( \"kokain\")' class='btn btn-primary w-50 align-self-center' type='button'>Kokain</button><br><button onclick='kjøp('hasj')' class='btn btn-primary w-50 align-self-center' type='button'>Hasj</button><br><button onclick='kjøp('amfetamin')' class='btn btn-primary w-50 align-self-center' type='button'>Amfetamin</button><br><button onclick='kjøp('benzo')' class='btn btn-primary w-50 align-self-center' type='button'>Benzo</button><br><button onclick='kjøp('molly')' class='btn btn-primary w-50 align-self-center' type='button'>Molly</button></div>"

这里我使用了反斜杠来转义双引号 示例:

 onclick='kjøp( \"kokain\")'

工作代码 - https://jsfiddle.net/jubish/ez3hr7g1/8/