gpt4 book ai didi

javascript - 使 链接激活展开文本按钮

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:00 25 4
gpt4 key购买 nike

我有一个看起来像这样的按钮:

<button class="button-global button-a" id="more" target="1">More Info</button>

它触发扩展文本:

$('.p2').hide();
$('#more').click(function (ev) {
var t = ev.target
$('#expanded' + $(this).attr('target')).toggle(50, function(){
console.log(ev.target)
$(t).html($(this).is(':visible')? 'Simplify' : 'More Info')
});
return false;
});

我想要这个链接(只有 aboutus 链接):

<a class="pcnav navlinks" href="#aboutus-target">About Us</a>

在单击时也触发扩展文本,但如果扩展已经可见则不触发文本,我不希望文本在 aboutus 上发生变化,就像它对按钮所做的那样,

(让新人更容易看到帖子,因为它变得有点困惑)

任何帮助都会很棒!

最佳答案

类驱动的 jQuery

多个<a> & <button>解决方案

每个<a> , <button> , & <section class='content...'>有:

  • 一个“公共(public)静态类”:.x

  • 两个“共享状态类”:.on.off

为了控制具有同一组类的不同标签,CSS/jQuery 选择器被声明为组合,从而为多个标签建立特定行为,同时共享一个类,允许轻松访问所有标签作为一个组。查看 Demo 2 的 CSS 部分中的注释以了解详细信息。


Fiddle

演示2

详情在演示中评论。

$('.more').on('click', function() {

// Get the #id of the closest article.p
var page = $(this).closest('.p').attr('id');

// Toggle .on/.off on all .x within article.p
$(`#${page} .x`).toggleClass('on off');
});

/*
Exclude .link.on to ensure click event is only
triggered when .off class is assigned to .link
*/
$('.link').not('.on').on('click', function() {

/*+ Expand .content located within the same .p

var page = $(this).closest('.p').attr('id');
$(`#${page} .x`).addClass('on').removeClass('off');

//-*/
//*- Expand .content located at this.href

var jump = $(this).attr('href');
$(`${jump} .x`).addClass('on').removeClass('off');

//-*/

// Scroll to this.href
$('html, body').animate({
scrollTop: $(jump).offset().top
}, 550);
});
html {
overflow-y: scroll;
overflow-x: hidden;
}

html,
body {
background: #fff;
padding: 20px;
font: 400 small-caps 16px/1.45 Arial;
}

main {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
}

article {
background: none;
display: table;
width: 100%;
}

nav,
section,
footer {
background: #222;
color: #fc3;
padding: 20px;
text-align: center;
font-size: 1rem;
margin: 0 auto;
width: 300px;
}

nav {
padding: 10px 20px;
margin: 0em auto -0.5em;
border-top-right-radius: 8px;
border-top-left-radius: 8px;
}

section p {
text-align: left;
}

footer {
padding: 15px 20px 10px;
margin: -2.8em auto 2.25em;
border-bottom-right-radius: 8px;
border-bottom-left-radius: 8px;
}

.link,
footer b {
font-size: 1.5rem;
color: #fc3;
}

nav b {
display: inline-block;
font-size: 1.5rem;
width: 1em;
}

button {
background: #fc3;
color: #000;
border: none;
border-radius: 5px;
padding: 8px 14px;
font: inherit;
font-size: 1.2rem;
cursor: pointer;
width: 150px;
}

/*
The next four rulesets demonstrate how only two
classes can be shared between multiple tags and still
provide different behavior and purpose.
*/

.content.off {
max-height: 0;
font-size: 0;
color: transparent;
opacity: 0;
transition: color 0.65s, max-height 0.95s, font-size 0.95s, opacity 2.5s;
}

.content.on {
height: auto;
max-height: 5000px;
font-size: 1.1rem;
color: #fc3;
opacity: 1;
transition: all 0.75s;
}

.more.off::before {
content: 'More Info';
}

.more.on::before {
content: 'Simplify';
}
<main id='doc'>

<article id='p1' class='p'>

<nav>
<a class="link x off" href="#p2">Next</a>
</nav>

<section>
<button class="more x off"></button>
</section>

<section class="content x off">
<p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>

<p>I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.</p>

<p>I am so happy, my dear friend, so absorbed in the exquisite...</p>
</section>

<footer>
<b>1</b>
</footer>

</article>

<article id='p2' class='p'>

<nav>
<a class="link x off" href="#p1">Prev</a>
<b>|</b>
<a class="link x off" href="#p3">Next</a>
</nav>

<section>
<button class="more x off"></button>
</section>

<section class="content x off">
<p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.</p>

<p>He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections.</p>

<p>The bedding was hardly...</p>
</section>
<footer>
<b>2</b>
</footer>
</article>

<article id='p3' class='p'>
<nav>
<a class="link x off" href="#p2">Prev</a>
</nav>

<section>
<button class="more x off"></button>
</section>

<section class="content x off">
<p>The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog.</p>

<p>Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs.</p>

<p>Waltz, bad nymph, for quick jigs vex! Fox nymphs grab quick-jived waltz. Brick quiz whangs jumpy veldt fox. Bright vixens jump; dozy fowl quack.</p>
</section>
<footer>
<b>3</b>
</footer>
</article>
</main>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


单例<a> & <button>解决方案

三元条件

以下演示将点击事件分别分配给按钮和链接。三元条件的结构如下:

variable = condition ? TRUE : FALSE;

.另外,.class ( .more ) 代替了 #id ( #more ) 作为更好的选择,但这不是必需的。


演示1

$('.content').hide();

$('.more').on('click', function() {
var txt = $(this).text() === 'Simplify' ? 'More Info' : 'Simplify';
$(this).text(txt);
$('.content').toggle();
});

$('.navLink').on('click', function() {
$('.content').show();
$('.more').text('Simplify');
});
<button class="more">More Info</button><br>

<a class="navLink" href="#about">About Us</a>

<p class='content'>
Content.
</p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


Ryan Wheale's 的一个分支风格很好Fiddle .

关于javascript - 使 <a> 链接激活展开文本按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com