首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SPFx WebPart - Tab

SPFx WebPart - Tab
EN

Stack Overflow用户
提问于 2021-04-24 12:13:41
回答 2查看 666关注 0票数 0

我对SPFX WebPart开发完全陌生。

我正在尝试开发一个Tab Web部件-- HTML似乎呈现得很好,但是Javascript没有启动(也许我使用的方式不合适)。

有些帮助会得到高度重视。

提前谢谢。

代码语言:javascript
复制
//The webpart.ts file
import { Version } from '@microsoft/sp-core-library';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';

import styles from './JqueryWebPart.module.scss';
import * as strings from 'JqueryWebPartStrings';
import MyTabTemplate from './MyTabTemplate';

import * as jQuery from 'jquery';
import 'jqueryui';
import { SPComponentLoader } from '@microsoft/sp-loader';

export interface IJqueryWebPartProps {
  description: string;
}

export default class JqueryWebPart extends BaseClientSideWebPart<IJqueryWebPartProps> {

  public constructor() {
    super();
  
    SPComponentLoader.loadCss('//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css');
  }

  public render(): void {
    this.domElement.innerHTML = MyTabTemplate .templateHtml;
    jQuery('.tabs', this.domElement);

    function openCity(evt, cityName) {
      // Declare all variables
      var i, tabcontent, tablinks;
    
      // Get all elements with class="tabcontent" and hide them
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
      }
    
      // Get all elements with class="tablinks" and remove the class "active"
      tablinks = document.getElementsByClassName("tablinks");
      for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" active", "");
      }
    
      // Show the current tab, and add an "active" class to the button that opened the tab
      document.getElementById(cityName).style.display = "block";
      evt.currentTarget.className += " active";
    }
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }

  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
  }
}

=================================================

代码语言:javascript
复制
//the template.ts file - holding the html
export default class MyTabTemplate {
    public static templateHtml: string = `
    <div class="tabs">    
        <!-- Tab links -->
        <div class="tab">
        <button class="tablinks" onclick="openCity(event, 'London')">London</button>
        <button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
        <button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
        </div>

        <!-- Tab content -->
        <div id="London" class="tabcontent">
        <h3>London</h3>
        <p>London is the capital city of England.</p>
        </div>

        <div id="Paris" class="tabcontent">
        <h3>Paris</h3>
        <p>Paris is the capital of France.</p>
        </div>

        <div id="Tokyo" class="tabcontent">
        <h3>Tokyo</h3>
        <p>Tokyo is the capital of Japan.</p>
        </div>
        </div>
    `;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-26 07:43:43

不能直接将函数openCity添加到呈现()中。您需要将其添加到脚本标记中,如下所示:

代码语言:javascript
复制
  public render(): void {
    this.domElement.innerHTML = MyTabTemplate.templateHtml;
    jQuery(".tabs", this.domElement);

    let head: any =document.getElementsByTagName("head")[0] || document.documentElement,
    script = document.createElement("script");
    script.type = "text/javascript";
    script.text =
      'function openCity(evt, cityName) { var i, tabcontent, tablinks;tabcontent = document.getElementsByClassName("tabcontent");for (i = 0; i < tabcontent.length; i++) {tabcontent[i].style.display = "none";}tablinks = document.getElementsByClassName("tablinks");for (i = 0; i < tablinks.length; i++) {tablinks[i].className = tablinks[i].className.replace(" active", "");}document.getElementById(cityName).style.display = "block";evt.currentTarget.className += " active";}';

    head.insertBefore(script, head.firstChild);
  }
票数 1
EN

Stack Overflow用户

发布于 2022-01-06 09:59:40

我们可以使用fluent UI 枢轴控件来开发选项卡We部件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67242651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档