Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将对象链接到jquery函数_Javascript - Fatal编程技术网

Javascript 将对象链接到jquery函数

Javascript 将对象链接到jquery函数,javascript,Javascript,我试图将JS中的一个对象链接到Jquery中的一个按钮函数。我已经将Jquery链接到我的HTML,因此它在技术上是可行的,然而,它是一个需要按钮来显示信息的特定项目。在对象中。我试着自己编辑它,但我一直被卡住,我不知道如何将两者联系起来。说明书也包括在内 // create a JavaScript object here with the following fields: firstName, lastName, jobTitle, homeOffice function Abo

我试图将JS中的一个对象链接到Jquery中的一个按钮函数。我已经将Jquery链接到我的HTML,因此它在技术上是可行的,然而,它是一个需要按钮来显示信息的特定项目。在对象中。我试着自己编辑它,但我一直被卡住,我不知道如何将两者联系起来。说明书也包括在内

 // create a JavaScript object here with the following fields: firstName, lastName, jobTitle, homeOffice
    function About(firstName, lastName, jobTitle, homeOffice, tellMeMore) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.jobTitle = jobTitle;
        this.homeOffice = homeOffice;
        this.tellMeMore = tellMeMore;
    };
    var about01 = new About("Megan", "Adams", "Customer Serice Rep", "Penn Field", "I have been working at in customer service since December 2018 and transferred over to the Resolutions Department in fall of 2018. In my spare time I love watching scary movies, listening to true crime podcasts and music, and making art.");

    // using jQuery and the object above, display the information as the appropriate button is clicked


    var button = document.querySelectorAll ('button');

    $(document).ready(function() {
        $(".button").click(function() {
            $(".name").fadeToggle(1000);
        });
    });

     $(document).ready(function() {
        $(".button1").click(function() {
            $(".job").fadeToggle(1000);
        });
    });

    $(document).ready(function() {
        $(".button2").click(function() {
            $(".office").fadeToggle(1000);
        });
    });

    $(document).ready(function() {
        $(".button3").click(function() {
            $(".more").fadeToggle(1000);
        });
    });



<!DOCTYPE html>
<html>
  <head>
     <title role="title">CEP Engineering Application</title>
     <link rel="stylesheet" type="text/css" href="styles.css" />
     <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
     <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  </head>
  <body>
        <article>
            <header role=banner>
         <h1>About Me</h1>
      </header>
                <img src="img/IMG_1989.jpg" alt="Megan Adams Picture" style="width:250px;height:460px; "class="img">
        <section>
         <button type="button" class="button">Name</button>
           <p class="name">Megan Adams</p>
        </section>
        <section>
           <button type="button" class="button1">Job Title</button>
           <p class="job">Customer Service Reo</p>
     </section>
     <section>
          <button type="button" class="button2">Home Office</button>
          <p class="office">Penn Field</p>
     </section>
     <section>
          <button type="button" class="button3">Tell Me More</button>
          <p class="more">I have been working at in customer service since December 2018 and transferred over to the Resolutions Department in fall of 2018. In my spare time I love watching scary movies, listening to true crime podcasts and music, and making art. </p>
      </section>
      <script src="init.js"></script>
          </article>
  </body>
</html>

您是否希望使用jQuery$selector.htmlvalue执行以下操作

//在这里创建一个包含以下字段的JavaScript对象:firstName、lastName、jobTitle、homeOffice 函数关于firstname、lastName、jobTitle、homeOffice、tellmore{ this.firstName=firstName; this.lastName=lastName; this.jobTitle=jobTitle; this.homeOffice=homeOffice; this.tellmore=tellmore; }; var about01=新AboutMegan,Adams,宾夕法尼亚州菲尔德的解决方案专家,自2018年12月起,我一直在HomeAway从事客户服务工作,并于2018年秋季转到解决方案部门。在我的业余时间,我喜欢看恐怖电影,听真正的犯罪播客和音乐,以及创作艺术。; $.name.html`${about01.firstName}${about01.lastName}`; $.job.htmlabout01.jobTitle; $.office.htmlabout01.homeOffice; $.more.htmlabout01.tellmore; //使用jQuery和上面的对象,在单击相应的按钮时显示信息 var button=document.queryselectoral'button'; $document.readyfunction{ $.button.clickfunction{ $.name.fadeToggle1000; }; }; $document.readyfunction{ $.button1.clickfunction{ $.job.fadeToggle1000; }; }; $document.readyfunction{ $.button2.clickfunction{ $.office.fadeToggle1000; }; }; $document.readyfunction{ $.button3.clickfunction{ $.more.fadeToggle1000; }; }; 钮扣{ 显示:块; } CEP工程应用 关于我 名称

Megan Adams

职位名称

客户服务Reo

内政部

Penn字段

告诉我更多

自2018年12月起,我一直在客户服务部工作,并于2018年秋季调到解决方案部。在我的业余时间,我喜欢看恐怖电影,听真正的犯罪播客和音乐,以及创作艺术


我编辑了代码和html,并使它们更小

现在看一看属性数据并阅读注释以了解其工作原理

//在这里创建一个包含以下字段的JavaScript对象:firstName、lastName、jobTitle、homeOffice 函数关于firstname、lastName、jobTitle、homeOffice、tellmore{ this.firstName=firstName; this.lastName=lastName; this.jobTitle=jobTitle; this.homeOffice=homeOffice; this.tellmore=tellmore; }; var about01=新AboutMegan,Adams,宾州菲尔德的客户服务代表,我自2018年12月起一直在客户服务部工作,并于2018年秋季转到解决方案部。在我的业余时间,我喜欢看恐怖电影,听真正的犯罪播客和音乐,以及创作艺术。; //使用jQuery和上面的对象,在单击相应的按钮时显示信息 var button=document.queryselectoral'button'; $document.readyfunction{ //呈现价值观, $section>p.eachfunction{ //更具活力的方法 var字段=$this.attrdata; var值=; 如果字段{ field.split.forEachx=>{ 如果值== 值=约01[x]; else值+=+about01[x]//firstName和lastName }; $this.htmlvalue } }; //现在,只需单击一个方法即可显示切换p $.button.clickfunction{ //你们知道p存在于按钮下 //因此,返回到当前对象的父对象,然后找到p并切换它。 $this.parent.findp.fadeToggle1000; }; }; 关于我 名称

职务

办公室

告诉我更多

只需要一个jQuery。您可以使用Array.prototype.keys获取对象的属性名,将当前元素的类名转换为.html中的小写。过滤当前元素的.indexOf className大于-1的属性名。映射并。加入结果

$function{ 函数关于firstname、lastName、jobTitle、homeOffice、tellmore{ this.firstName=firstName; this.lastName=lastName; this.jobTitle=jobTitle; this.homeOffice=homeOffice; this.tellmore=tellmore; }; 康斯塔博 ut01=新AboutMegan,Adams,宾州菲尔德的客户服务代表,我自2018年12月起一直在客户服务部工作,并于2018年秋季调到解决方案部。在我的业余时间,我喜欢看恐怖电影,听真正的犯罪播客和音乐,以及创作艺术。; const keys=Object.keysabout01; $.name、.job、.office、.more.htmlfunction{ const el=这个; 返回keys.filterfunctionvalue{ 返回值.toLowerCase .indexOfel.className>-1 } .mapfunctionvalue{ 返回大约01[值] }.加入 } } 您只需要在JS中使用一个document.ready方法

要实现这一点,您可以向所有按钮添加一个单击事件侦听器。然后,单击按钮时,可以使用$this引用单击的按钮。要向其添加文本的元素是单击的按钮旁边的段落。您可以使用$this.next'p获取段落元素。然后,使用段落的类名可以确定要显示的对象属性

下面,我使用了一个名为classToProp的对象,它将类名映射到从About对象检索到的字符串。使用此选项,可以显示要添加到相邻p标记的特定信息

请参见下面的工作示例:

函数关于firstname、lastName、jobTitle、homeOffice、tellmore{ this.firstName=firstName; this.lastName=lastName; this.jobTitle=jobTitle; this.homeOffice=homeOffice; this.tellmore=tellmore; }; var about01=新AboutMegan,Adams,宾州菲尔德的客户服务代表,我自2018年12月起一直在客户服务部工作,并于2018年秋季转到解决方案部。在我的业余时间,我喜欢看恐怖电影,听真正的犯罪播客和音乐,以及创作艺术。; 变量classToProp={ 名称:about01.firstName++about01.lastName, 职务:约01.1职务, 办公地点:办公地点:, 更多信息:大约31.5分钟 } $document.readyfunction{ $button.clickfunction{ var nextP=$this.next'p'; var className=nextP.attrclass; var txt=nextP.text;//从段落中获取文本 nextP.texttxt?:classToProp[className];//如果文本为空,则显示对象的关联属性,否则,如果对象已包含文本,则将其置为空-这允许切换效果 }; } CEP工程应用 关于我 名称

职位名称

内政部

告诉我更多


应在何时何地显示对象的值?注意,不需要多个.ready调用。您希望它控制台记录信息,还是更改按钮html?您的html代码也会有所帮助。对象的值需要显示在按钮中,并且需要在单击按钮时显示。我将为.name类添加应该出现的HTMLW,其中firstName和lastName属性都存在于对象中?我将尝试!非常感谢。我在HTML中添加了。我的代码对你有用吗?如果没有,我应该改变什么呢?所以,我需要每个按钮都显示名称,然后单击它会显示我的名称,等等。我已经将它链接到我的HTML,所以,从技术上讲,它是可行的,但是,我们应该将它连接到对象。这是一个屏幕视频,它应该是什么样子的谢谢!这就是我要找的。我会试试这个。很高兴能帮上忙。