javascript - 通过启用特定选项卡直接加载网页

标签 javascript jquery django html css

我是前端技术新手,需要以下方面的帮助。

我正在 Django 中制作一个 Web 应用程序,我的网页有 3 个选项卡,我正在尝试实现以下目标:-

问题 1) 我想加载通过打开直接网址启用的网页特定选项卡。

示例:当我提交 work2 表单时,我想加载包含填充数据的页面,但默认启用“work2”(而不是 work 1)选项卡。 问题是关于如何启用特定选项卡,我已经尝试过这样的事情

本地主机:8000/project/#work2

但是它不起作用。

问题 2)在我的 CSS 文件中,我有

.tab-content > div:last-child {
  display: none;

这会加载初始页面,并在初始页面上启用 2 个选项卡(一个接一个),但隐藏第三个选项卡(因为最后一个子选项被标记为“显示:无”)。如何隐藏第二个选项卡并默认仅显示第一个选项卡详细信息?

这是 html 代码:-

<div class="form">

<ul class="tab-group">
    <li class="tab active"><a href="#work1">Work1</a></li>
    <li class="tab"><a href="#work2">Work2</a></li>
    <li class="tab "><a href="#work3">Work3</a></li>
</ul>

<div class="tab-content">

    <div id="work1">
        <h1>Some Data here</h1>
        <form action="/" method="get">
        ...
        </form>
    </div>

    <div id="work2">
       <h1>Some Data here </h1>
       <form action="/" method="get">
       ...
        </form>
    </div>

    <div id="work3">
       <h1>Some Data here</h1>
       <form action="/" method="get">
       ...
        </form>
    </div>
</div>
</div>

这是我的 js 文件:-

$(document).ready(function() {
$('.form').find('input, textarea').on('keyup blur focus', function (e) {

var $this = $(this),
  label = $this.prev('label');

  if (e.type === 'keyup') {
        if ($this.val() === '') {
      label.removeClass('active highlight');
    } else {
      label.addClass('active highlight');
    }
} else if (e.type === 'blur') {
    if( $this.val() === '' ) {
        label.removeClass('active highlight');
        } else {
        label.removeClass('highlight');
        }
} else if (e.type === 'focus') {

  if( $this.val() === '' ) {
        label.removeClass('highlight');
        }
  else if( $this.val() !== '' ) {
        label.addClass('highlight');
        }
}

});

$('.tab a').on('click', function (e) {

e.preventDefault();

$(this).parent().addClass('active');
$(this).parent().siblings().removeClass('active');

var target = $(this).attr('href');

$('.tab-content > div').not(target).hide();

$(target).fadeIn(600);

});

});

这是我完整的 CSS 文件:-

*, *:before, *:after {
  box-sizing: border-box;
}

html {
  overflow-y: scroll;
}

body {
  background: #c1bdba;
  font-family: 'Titillium Web', sans-serif;
}

a {
  text-decoration: none;
  color: #1ab188;
  -webkit-transition: .5s ease;
  transition: .5s ease;
}
a:hover {
  color: #179b77;
}

.form {
  background: rgba(19, 35, 47, 0.9);
  padding: 40px;
  max-width: 50%;
  margin: 40px auto;
  border-radius: 4px;
  box-shadow: 0 4px 10px 4px rgba(19, 35, 47, 0.3);
}

.tab-group {
  list-style: none;
  padding: 0;
  margin: 0 0 40px 0;
}
.tab-group:after {
  content: "";
  display: table;
  clear: both;
}
.tab-group li a {
  display: block;
  text-decoration: none;
  padding: 15px;
  background: rgba(160, 179, 176, 0.25);
  color: #a0b3b0;
  font-size: 20px;
  float: left;
  width: 33%;
  text-align: center;
  cursor: pointer;
  -webkit-transition: .5s ease;
  transition: .5s ease;
}
.tab-group li a:hover {
  background: #179b77;
  color: #ffffff;
}
.tab-group .active a {
  background: #1ab188;
  color: #ffffff;
}

.tab-content > div:last-child {
  display: none;
}

h1 {
  text-align: center;
  color: #ffffff;
  font-weight: 300;
  margin: 0 0 40px;
}

label {
  position: absolute;
  -webkit-transform: translateY(6px);
          transform: translateY(6px);
  left: 13px;
  color: rgba(255, 255, 255, 0.5);
  -webkit-transition: all 0.25s ease;
  transition: all 0.25s ease;
  -webkit-backface-visibility: hidden;
  pointer-events: none;
  font-size: 22px;
}
label .req {
  margin: 2px;
  color: #1ab188;
}

label.active {
  -webkit-transform: translateY(50px);
          transform: translateY(50px);
  left: 2px;
  font-size: 14px;
}
label.active .req {
  opacity: 0;
}

label.highlight {
  color: #ffffff;
}

input, textarea {
  font-size: 22px;
  display: block;
  width: 100%;
  height: 10%;
  padding: 5px 10px;
  background: none;
  background-image: none;
  border: 1px solid #a0b3b0;
  color: #ffffff;
  border-radius: 0;
  -webkit-transition: border-color .25s ease, box-shadow .25s ease;
  transition: border-color .25s ease, box-shadow .25s ease;
}
input:focus, textarea:focus {
  outline: 0;
  border-color: #1ab188;
}

textarea {
  border: 2px solid #a0b3b0;
  resize: vertical;
}

.field-wrap {
  position: relative;
  margin-bottom: 40px;
}

.top-row:after {
  content: "";
  display: table;
  clear: both;
}
.top-row > div {
  float: left;
  width: 48%;
  margin-right: 4%;
}
.top-row > div:last-child {
  margin: 0;
}

.button {
  border: 0;
  outline: none;
  border-radius: 0;
  padding: 15px 0;
  font-size: 2rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .1em;
  background: #1ab188;
  color: #ffffff;
  -webkit-transition: all 0.5s ease;
  transition: all 0.5s ease;
  -webkit-appearance: none;
}
.button:hover, .button:focus {
  background: #179b77;
}

.button-block {
  display: block;
  width: 100%;
}

.forgot {
  margin-top: -20px;
  text-align: right;
}

最佳答案

看来您正在使用jquery,所以我将假设并使用其中的一些。我对你的 CSS 做了一个小改动:

.tab-content > div {
  display: none;
}

以便默认隐藏所有“某些数据”。

然后我更改了您的 JavaScript,以便它从 url 中提取哈希参数(即“#work2”)并更改任何适合可见的“某些数据”:

var selectedTab = location.hash

function switchTabs(selectedTab){
    if (selectedTab == "")
    {
        selectedTab = "#work1";
    }

    $('.tab-content > div').not(selectedTab).hide();

    $(selectedTab).fadeIn(600);
}

switchTabs(selectedTab);

$('.tab a').on('click', function(e) {

    e.preventDefault();

    $(this).parent().addClass('active');
    $(this).parent().siblings().removeClass('active');

    var target = $(this).attr('href');

    switchTabs(target)
});

关于javascript - 通过启用特定选项卡直接加载网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34709130/

相关文章:

javascript - CSS 中的伪元素

javascript - 我应该使用什么工具来查看客户端通过 AJAX 发生的 GET 和 POST 请求的详细信息?

php - jquery ui 对话框和加载外部内容

javascript - for循环和setInterval,幻灯片展示

django - 使用主干/ Ember 使 Django 成为一个简单的 REST API?

javascript - 无法使用它的属性获取 xml 元素的内部 HTML

javascript - 在Java中生成唯一键

python - 以默认 ID 作为参数包含的不起作用

javascript - 在平板电脑上放大和放大时,如何阻止包含 Kendo UI dataviz 图表的 div 元素与其他元素重叠

python - 如何阻止 Django 转义 # 符号