使用当前标签

时间:2017-02-22 17:56:31

标签: javascript

我已经尝试了一些东西,但是在编码方面还不够精通,显然

我在这里设置了一个jsfiddle - https://jsfiddle.net/7ojfmxn2/17/

我有一个显示选项卡功能以显示下面的标签内容,我有标签的标题文本,我想在每次点击一个标题时将该文本更改为当前标签的名称“联盟选项卡”我' d喜欢重命名为Tab 0,Tab 1,Tab 2等,每当选择一个,有什么帮助吗?

function show_tab(tab_id) {
  var done = false;
  var counter = 0;
  while (!done) {
    var this_tab_content = document.getElementById("tabcontent" + counter);
    var this_tab = document.getElementById("tab" + counter);
    if (!this_tab_content) {
      done = true;
    } else {
      if (counter == tab_id) {
        this_tab_content.style.display = '';
        this_tab.className = "currenttab";
      } else {
        this_tab_content.style.display = 'none';
        this_tab.className = "";
      }
    }
    counter++;
  }
  location.hash = tab_id;
}

if (location.hash) {
  show_tab(location.hash.substr(1));
} else {
  show_tab(0);
}

3 个答案:

答案 0 :(得分:1)

在您的HTML id标记中添加<title>,如下所示:

<title id="myTitle"></title>

使用jQuery你可以这样做:

$('#myTitle').html(this_tab.className);

使用JavaScript,它将是

document.getElementById("myTitle").innerText = this_tab.className;

答案 1 :(得分:1)

您需要在包含选项卡的元素的标题上设置id,以便稍后选择它并更改其内容。

然后您可以将第二个参数传递给show_tab函数,该函数将是您要显示的标题标题。

以下是更新的代码(由于帖子大小限制,我必须删除CSS以便在此处发布)

这是所有CSS https://jsfiddle.net/7ojfmxn2/39/

的工作小提琴

function show_tab(tab_id, tab_title) {
  var done = false;
  var counter = 0;
  while (!done) {
    var this_tab_content = document.getElementById("tabcontent" + counter);
    var this_tab = document.getElementById("tab" + counter);
    if (!this_tab_content) {
      done = true;
    } else {
      if (counter == tab_id) {
        this_tab_content.style.display = '';
        this_tab.className = "currenttab";
      } else {
        this_tab_content.style.display = 'none';
        this_tab.className = "";
      }
    }
    counter++;
  }
  location.hash = tab_id;
  document.getElementById('tab_title').innerHTML = tab_title || 'LEAGUE TABS';
}

if (location.hash) {
  show_tab(location.hash.substr(1));
} else {
  show_tab(0);
}
<div id="contentframe">
    <div id="withmenus">

      <div class="myfantasyleague_tabmenu"><span id="tab_title">LEAGUE TABS</span>
        <input id="sub100" type="checkbox">
        <label for="sub100"><span></span></label>
        <ul id="homepagetabs">
          <li id="tab0" onclick="javascript:show_tab('0', 'Tab 0');"><a class="no-sub">Tab 0<input id="sub100" type="checkbox"><label for="sub100"></label></a></li>
          <li id="tab1" onclick="javascript:show_tab('1', 'Tab 1');"><a class="no-sub">Tab 1<input id="sub100" type="checkbox"><label for="sub100"></label></a></li>
          <li id="tab2" onclick="javascript:show_tab('2', 'Tab 2');"><a class="no-sub">Tab 2<input id="sub100" type="checkbox"><label for="sub100"></label></a></li>
          <li id="tab3" onclick="javascript:show_tab('3', 'Tab 3');"><a class="no-sub">Tab 3<input id="sub100" type="checkbox"><label for="sub100"></label></a></li>
        </ul>
      </div>

      <div id="tabcontent0" class="homepagetabcontent">
        <div class="mobile-wrap">
          <table align="center" cellspacing="1" class="homepagemodule report" id="brief_standings">
            <caption><span>Tab 0 CONTENT</span></caption>
            <tbody>
              <tr>
                <td id="division00" colspan="3">
                  <h3>Division 1</h3></td>
              </tr>
              <tr>
                <th class="fname" title="Franchise Name">Franchise</th>
                <th class="h2hwlt" title="Overall Wins, Losses and Ties">W&#8209;L&#8209;T</th>
                <th class="pf" title="Points For (Total Year-to-Date Point Scored)">PF</th>
            </tbody>
          </table>
        </div>
      </div>

      <div id="tabcontent1" class="homepagetabcontent">
        <div class="mobile-wrap">
          <table align="center" cellspacing="1" class="homepagemodule report" id="brief_standings">
            <caption><span>Tab 1 CONTENT</span></caption>
            <tbody>
              <tr>
                <td id="division00" colspan="3">
                  <h3>Division 1</h3></td>
              </tr>
              <tr>
                <th class="fname" title="Franchise Name">Franchise</th>
                <th class="h2hwlt" title="Overall Wins, Losses and Ties">W&#8209;L&#8209;T</th>
                <th class="pf" title="Points For (Total Year-to-Date Point Scored)">PF</th>
            </tbody>
          </table>
        </div>
      </div>

      <div id="tabcontent2" class="homepagetabcontent">
        <div class="mobile-wrap">
          <table align="center" cellspacing="1" class="homepagemodule report" id="brief_standings">
            <caption><span>Tab 2 CONTENT</span></caption>
            <tbody>
              <tr>
                <td id="division00" colspan="3">
                  <h3>Division 1</h3></td>
              </tr>
              <tr>
                <th class="fname" title="Franchise Name">Franchise</th>
                <th class="h2hwlt" title="Overall Wins, Losses and Ties">W&#8209;L&#8209;T</th>
                <th class="pf" title="Points For (Total Year-to-Date Point Scored)">PF</th>
            </tbody>
          </table>
        </div>
      </div>

      <div id="tabcontent3" class="homepagetabcontent">
        <div class="mobile-wrap">
          <table align="center" cellspacing="1" class="homepagemodule report" id="brief_standings">
            <caption><span>Tab 3 CONTENT</span></caption>
            <tbody>
              <tr>
                <td id="division00" colspan="3">
                  <h3>Division 1</h3></td>
              </tr>
              <tr>
                <th class="fname" title="Franchise Name">Franchise</th>
                <th class="h2hwlt" title="Overall Wins, Losses and Ties">W&#8209;L&#8209;T</th>
                <th class="pf" title="Points For (Total Year-to-Date Point Scored)">PF</th>
            </tbody>
          </table>
        </div>
      </div>
    </div>

  </div>

答案 2 :(得分:1)

我不确定这是你要求的,但如果你想用Tab名称替换“League Tabs”,你可以这样做

1-为存储选项卡标题的范围提供ID,例如id = tabheader

2-在您设置可见选项卡的代码部分中,还设置该范围的内容(具有id = tabheader的元素)

这是代码

HTML

...
<div class="myfantasyleague_tabmenu"><span id="tabheader">LEAGUE TABS</span>
....

JS

....
 if (counter == tab_id) {
        this_tab_content.style.display = '';
        this_tab.className = "currenttab";
        var tabheader = document.getElementById("tabheader");
        tabheader.innerHTML="tab " + counter;
      }
...

查看更新的小提琴 https://jsfiddle.net/7ojfmxn2/30/

修改

如果您希望标题LEAGUE TABS在开头显示,您必须以不选择标签的方式评论您的初始化代码。

/*
if (location.hash) {
  show_tab(location.hash.substr(1));
} else {
  show_tab(0);
}
*/

编辑2 为避免显示所有标签,您可以使用

         show_tab(-1);

而不是

        show_tab(0);

请参阅小提琴https://jsfiddle.net/7ojfmxn2/50/

编辑3

如果您希望在页面加载时看到“主”标签名称,无论如何加载标签0,您可以

1-更改show_tab,添加输入参数。此布尔参数确定要显示的内容(主选项卡名称或显示的选项卡名称),如下所示

 function show_tab(tab_id, show_main_tab_name) {
   .....
   //here is where the tab title is displaied 
   var tabheader = document.getElementById("tabheader");
    if(show_main_tab_name){
        tabheader.innerHTML="LEAGUE TABS";
    }
    else {
       tabheader.innerHTML="tab " + counter;
    }
    ....

在初始化show选项卡时调用param设置为true  (我把你原来的功能)

if (location.hash) {
  show_tab(location.hash.substr(1));
} else {
  show_tab(0,true);
}

看到我的新小提琴https://jsfiddle.net/7ojfmxn2/55/