简体   繁体   English

此JavaScript可以处理WordPress页面中的多个显示/隐藏吗?

[英]Can this JavaScript handle more than one show/hide in a WordPress Page?

There are other questions about this with great Answers (for example storeitem crops up) but unfortunately I'm not familiar enough yet to do more than tweak the code we've got. 关于此问题,还有其他一些很好的答案(例如,存储项目增加了),但是不幸的是,我除了调整我们已经获得的代码之外还不够熟悉。 I've asked in other forums without success. 我曾在其他论坛上要求没有成功。

We have a WordPress (hosted) site and a Page (not Post) needing four plain copy text paragraphs that simply show/hide some additional plain copy text on a 'More...' click. 我们有一个WordPress(托管)站点和一个页面(不是Post),需要四个纯文本段落,这些段落只需单击“更多...”即可显示/隐藏一些其他纯文本。

Visually on the Page there's four paragraphs of plain copy test, each followed by 'More...' - nothing fancier than that. 在页面上直观地看到有四个段落的纯文本测试,每个段落后面都带有“更多...”-没什么比这更奇特的了。 There are no panels, borders, graphical elements etc. 没有面板,边框,图形元素等。

The idea is that a user can expand one, some or all of the paras. 这个想法是用户可以扩展一个,一些或所有参数。

The code we have works fine for one instance (one 'More...') if we start the Page (within WP editor) with this JavaScript: 如果我们使用此JavaScript启动Page(在WP编辑器中),我们拥有的代码对于一个实例(一个'More ...')就可以正常工作:

<script type="text/javascript" language="javascript">// <![CDATA[
function toggle() {
var ele = document.getElementById("toggleText");
var text = document.getElementById("displayText");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Show";
}
else {
ele.style.display = "block";
text.innerHTML = "Hide";
}
}
// ]]></script>

Then wrap the to-be-revealed content: 然后包装要显示的内容:

<div id="toggleText" style="display: none;">to-be-revealed content</div>

Then make the click: 然后单击:

<a id="displayText" href="javascript:toggle();">More...</a>

That works perfectly. 那很好。 The to-be-revealed content just snaps up and content beneath it follows below. 即将公开的内容只是抢购而下的内容如下。

BUT taking the advice we have so far on multiple instances in one Page doesn't work. 但是,到目前为止,我们在一页中的多个实例上获得建议是行不通的。

The advice is to repeat the JavaScript and use different IDs for toggleText and displayText in each repetition. 建议是重复JavaScript,并在每次重复中为toggleText和displayText使用不同的ID。

But doing that, the last copy of the JavaScript in the Page is always the one that triggers, showing to-be-revealed content in the final (fourth) para whichever 'More...' is clicked. 但是这样做的话,页面中JavaScript的最后一个副本始终是触发的那个副本,无论单击了“更多...”,都在最后(第四)段中显示了要显示的内容。 (I think I can see why - it's the 'latest' toggle function so it's the one that applies.) (我想我可以理解为什么-这是“最新的”切换功能,因此是适用的功能。)

This is what we have so far for multiple instances (and the person who advised us isn't about...) - it's just four copies of the JS with toggleText and displayText numbered: 到目前为止,这是我们为多个实例准备的(并且向我们提出建议的人并不在意...)-只是JS的四个副本,其toggleText和displayText编号为:

<script type="text/javascript" language="javascript">// <![CDATA[
function toggle() { var ele = document.getElementById("toggleText1"); var text =      document.getElementById("displayText1"); if(ele.style.display == "block") { ele.style.display =    "none"; text.innerHTML = "More ..."; } else { ele.style.display = "block"; text.innerHTML = "... less"; } }
// ]]></script>

<script type="text/javascript" language="javascript">// <![CDATA[
function toggle() { var ele = document.getElementById("toggleText2"); var text =  document.getElementById("displayText2"); if(ele.style.display == "block") { ele.style.display = "none"; text.innerHTML = "More ..."; } else { ele.style.display = "block"; text.innerHTML = "... less"; } }
// ]]></script>

<script type="text/javascript" language="javascript">// <![CDATA[
function toggle() { var ele = document.getElementById("toggleText3"); var text =  document.getElementById("displayText3"); if(ele.style.display == "block") { ele.style.display = "none"; text.innerHTML = "More ..."; } else { ele.style.display = "block"; text.innerHTML = "... less"; } }
// ]]></script>

<script type="text/javascript" language="javascript">// <![CDATA[
function toggle() { var ele = document.getElementById("toggleText4"); var text = document.getElementById("displayText4"); if(ele.style.display == "block") { ele.style.display = "none"; text.innerHTML = "More ..."; } else { ele.style.display = "block"; text.innerHTML = "... less"; } }
// ]]></script>

Then the wrap and click are each numbered for each instance too, eg: 然后,每个实例的包装和单击也分别编号,例如:

<a id="displayText1" href="javascript:toggle();"><span style="color: #339966;">Show more ...</span></a>
<div id="toggleText1" style="display: none;">
Content
</div>

QUESTION: can this way of using JavaScript ever work for multiple instances? 问题:这种使用JavaScript的方式是否可以在多个实例中使用? If so please show! 如果是这样,请显示!

It looks as though there needs to be some sort of ID for the toggle function itself to tie it to one ('numbered') instance. 似乎切换功能本身需要某种ID才能将其绑定到一个(“编号”)实例。 I can see how some of the other answers point to relevant solutions but unfortunately I don't know enough yet to make proper sense of them (touching jQuery would be a first for me and hoping to have this working on Monday). 我可以看到其他一些答案如何指向相关的解决方案,但是不幸的是,我对这些解决方案的理解还不够了解(接触jQuery对我来说是第一个,希望能在星期一进行)。

Thanks, C 谢谢,C

You can try 你可以试试

<a id="displayText" href="javascript:toggle(this);">More...</a>

then in toggle function 然后在切换功能

function toggle(element) {
    // element here is the link you clicked try to find the hidden text relative to this link. using Dom navigation
}

I don't see any need to ever create multiple functions that do the same thing. 我认为没有必要创建执行相同功能的多个功能。 In general you should pass parameters in the function call. 通常,您应该在函数调用中传递参数。 eg 例如

<a id="displayText1" href="javascript:toggle(1);">...</a>
<a id="displayText2" href="javascript:toggle(2);">...</a>
<a id="displayText3" href="javascript:toggle(3);">...</a>
<a id="displayText4" href="javascript:toggle(4);">...</a>

and in the javascript: 并在javascript中:

function toggle(num) {
    var ele = document.getElementById("toggleText" + num);
    var text = document.getElementById("displayText" + num);
    ....
}

Works like a charm. 奇迹般有效。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM