为了账号安全,请及时绑定邮箱和手机立即绑定

用文字改变图像

用文字改变图像

慕容森 2023-11-02 19:58:06
我正在制作一款游戏,根据玩家所做的选择,它将显示新的文本供选择。我正在尝试将图像与该文本连接起来,以便它们能够适应每个选择。现在,我的文字可以正常工作,但我的图像无法显示。我在这里做错了什么?  <script type ="text/javascript">    const textElement = document.getElementById('text')    const optionButtonsElement = document.getElementById('option-      buttons')        let state = {}    function startGame() {        state = {}        showTextNode(1)                         }      function showTextNode(textNodeIndex) {      const textNode = textNodes.find(textNode => textNode.id ===      textNodeIndex)      textElement.innerText = textNode.text      document.getElementById('img').src=textNode.img      while (optionButtonsElement.firstChild) {         optionButtonsElement.removeChild(optionButtonsElement.firstChild)        }        textNode.options.forEach(option => {            if (showOption(option)) {            const button = document.createElement('button')            button.innerText = option.text            button.classList.add('btn')            button.addEventListener('click', () =>             selectOption(option))            optionButtonsElement.appendChild(button)                }            })         }        function showOption(option) {        return option.requiredState == null ||         option.requiredState(state)            }        function selectOption(option) {        const nextTextNodeId = option.nextText        if (nextTextNodeId <= 0) {        return startGame()            }        state = Object.assign(state, option.setState)        showTextNode(nextTextNodeId)            }              const textNodes = [        {        id: 1,        Image:url('invitation.jpg'),        text: "You are cordially invited to celebrate Sir Troy         Bennet's 70th birthday! RSVP to attend the event this         evening at the Cherry Hill mansion.",        options: [        {        text: 'RSVP',        nextText: 2        },        {        text: "I'm going to stay home",        nextText: 24        }         ]          },
查看完整描述

1 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

这将需要更多上下文,例如 JSFIDDLE 或 CODEPEN 上的可重现示例...但是根据我所看到的代码所说;

textNode.img

当你在寻找时;

textNode.Image

17号线;

document.getElementById('img').src=textNode.img

所以你必须像上面提到的那样改变Image:url('invitation.jpg'),为beimg:url('invitation.jpg'),或更新textNode.img为be textNode.Image


查看完整回答
反对 回复 2023-11-02
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信