【问题标题】:Java Script Objects calling in Index.HTML file在 Index.HTML 文件中调用的 Javascript 对象
【发布时间】:2025-11-23 12:30:02
【问题描述】:

所以,我创建了一个 HTML 文件,其中包含带有以下标签的按钮:名字、姓氏等 我需要对按钮进行编程,以便当用户单击它时,拳头名称按钮应该消失,而在 <div> 中,您会看到名称“John Doe”

我在我的 .js 文件中创建了这个:

// create a JavaScript object here with the following fields: firstName, lastName, jobTitle, homeOffice

var demo = {
    firstName : "Waseem",
    lastName : "Qazi",
    jobTitle : "Traveler Care",
    homeOffice : "Penn. field",
    tellMeMore : "Exicted to re-start my coding adventure again. Been a while 
    since I had coded.  Looking forward to all the learning and growth this 
    amazing oppportunity will present."

    givefirstname : function() {
        return this.firstName;
    };

使用 jQuery 和上面的对象,在单击相应按钮时显示信息。

如何在 HTML 文件中调用它,以便每个函数在屏幕上返回相应的数据?

我在 HTML 文件中的调用行:

<li>
    <a class="selected" href="">
        <button type="button" onclick=demo.givefirstname>
          First Name
        </button> 
    </a>
</li>
<br>

【问题讨论】:

  • 你不能把&lt;button&gt; 放在&lt;a&gt; 里面。见*.com/questions/6393827/…
  • 你说你想把名字放在&lt;div&gt;里面。您的 HTML 中没有 DIV。

标签: javascript html object methods


【解决方案1】:

您可以替换或隐藏/显示一些 dom 元素。我会选择后者。我还将把giveFirstName 方法变成showValue 方法,这样它就更灵活了。

var demo = {
  firstName : "Waseem",
  lastName : "Qazi",
  jobTitle : "Traveler Care",
  homeOffice : "Penn. field",
  tellMeMore : "Exicted to re-start my coding adventure again. Been a while since I had coded.  Looking forward to all the learning and growth this amazing oppportunity will present.",

  showValue : function(key, el) {
    el.getElementsByTagName('span')[0].style.display = 'none';
    var paragraph = el.getElementsByTagName('span')[1];
    paragraph.style.display = 'inline';
    paragraph.innerHTML = this[key];
  },
}
p.value {
  display: none;
}
<li>
    <a class="selected" onclick="demo.showValue('firstName', this)">
        <span class="title">First Name</span>
        <span class="value"></span>
    </a>
</li>

【讨论】:

    【解决方案2】:

    您需要调用该函数,并且您需要添加将按钮替换为返回值的代码。

    在下面的代码中,我将&lt;a&gt; 更改为&lt;div&gt;,因为锚点内不能有按钮,单击锚点会尝试跟随链接。

    var demo = {
      firstName: "Waseem",
      lastName: "Qazi",
      jobTitle: "Traveler Care",
      homeOffice: "Penn. field",
      tellMeMore: `Exicted to re-start my coding adventure again. Been a while 
      since I had coded.Looking forward to all the learning and growth this
      amazing oppportunity will present.`,
      givetitle: function() {
        return this.jobTitle;
      },
      givefirstname: function() {
        return this.firstName;
      },
      givelastname: function() {
        return this.lastName;
      }
    };
    
    function replaceMe(element, contents) {
      element.parentElement.innerHTML = contents;
    }
    <ul>
      <li>
        <div class="selected">
          <button type="button" onclick="replaceMe(this, demo.givefirstname())">
              First Name
            </button>
        </div>
      </li>
      <li>
        <div class="selected">
          <button type="button" onclick="replaceMe(this, demo.givelastname())">
              Last Name
            </button>
        </div>
      </li>
      <li>
        <div class="selected">
          <button type="button" onclick="replaceMe(this, demo.givetitle())">
              Job Title
            </button>
        </div>
      </li>
    </ul>

    【讨论】:

    • 太棒了!谢谢一堆!但是,当我为其余函数复制相同的代码以调用姓氏、职务等时,HTML 文件不会执行任何操作。没有任何按钮更改。只有当我单独使用一个功能时,页面才能为那个按钮工作。但是当我复制其余按钮的代码时。一个都没有。嗯
    • 我也应该有单独的 HTML 和 JS 文件,所以我将你的第一个代码块添加到 JS 文件,将第二个代码块添加到 HTML。
    • 我已经为其他字段添加了更多按钮并且它有效。也许您在执行此操作时犯了语法错误。你检查控制台是否有错误?
    • 这应该适用于单独的 HTML 和 JS 文件。