admin管理员组文章数量:1414628
I implemented a collapsible button, it works well in WP, but if I insert it in a ul
list it doesn't work, that is the button does not open.
Then I tried to run the code in jsfiddle and there it does work.
The only difference between the WP code and the jsfiddle code is in the javascript
- on WP the
var content
is defined bythis.parentElement.nextElementSibling;
- on jsfiddle the
var content
is defined bythis.nextElementSibling;
This is due to the fact that
- by removing
parentElement
on WP the button will break - by adding
parentElement
on jsfiddle the button will break
Just for information, the javascript on WP is loaded in the footer.
Is it possibile to fix the problem? Here is the jsfiddle demo.
Below you can see jsfiddle vs WP
HTML FROM CHROME INSPECT TOOL
<div class="entry-content">
<p>Does <button class="col">this</button> work?</p>
<div class="con space" style="">
<p>Yes!</p>
<p></p></div>
<hr>
<ul>
<li>Does <button class="col">this</button> work?
<div class="con space">
<p>Only in jsfiddle, not in WP!</p>
</div>
</li>
<li style="">another line</li>
</ul>
</div>
HTML FROM WP CLASSIC EDITOR
Does <button class=col>this</button> work?
<div class="con space"><p>Yes!<p/></div>
<hr>
<ul>
<li>Does <button class=col>this</button> work?
<div class="con space"><p>Only in jsfiddle, not in WP!</p></div></li>
<li>another line</li>
</ul>
The space
class just contains margin-bottom: 1.5em;
I implemented a collapsible button, it works well in WP, but if I insert it in a ul
list it doesn't work, that is the button does not open.
Then I tried to run the code in jsfiddle and there it does work.
The only difference between the WP code and the jsfiddle code is in the javascript
- on WP the
var content
is defined bythis.parentElement.nextElementSibling;
- on jsfiddle the
var content
is defined bythis.nextElementSibling;
This is due to the fact that
- by removing
parentElement
on WP the button will break - by adding
parentElement
on jsfiddle the button will break
Just for information, the javascript on WP is loaded in the footer.
Is it possibile to fix the problem? Here is the jsfiddle demo.
Below you can see jsfiddle vs WP
HTML FROM CHROME INSPECT TOOL
<div class="entry-content">
<p>Does <button class="col">this</button> work?</p>
<div class="con space" style="">
<p>Yes!</p>
<p></p></div>
<hr>
<ul>
<li>Does <button class="col">this</button> work?
<div class="con space">
<p>Only in jsfiddle, not in WP!</p>
</div>
</li>
<li style="">another line</li>
</ul>
</div>
HTML FROM WP CLASSIC EDITOR
Does <button class=col>this</button> work?
<div class="con space"><p>Yes!<p/></div>
<hr>
<ul>
<li>Does <button class=col>this</button> work?
<div class="con space"><p>Only in jsfiddle, not in WP!</p></div></li>
<li>another line</li>
</ul>
The space
class just contains margin-bottom: 1.5em;
1 Answer
Reset to default 1Solution provided by Steven
<script type="text/javascript">
( function() {
coll = document.getElementsByClassName("col");
conn = document.getElementsByClassName("con");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].setAttribute('data-id', 'con' + i);
conn[i].setAttribute('id', 'con' + i);
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = document.getElementById(this.getAttribute('data-id'));
if (content.style.maxHeight) {
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
} )();
</script>
本文标签: cssCollapsible button inside a ul list does work in jsfiddle but not in WP
版权声明:本文标题:css - Collapsible button inside a ul list does work in jsfiddle but not in WP 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745157112a2645240.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
style.css
and other minor things. Now I post the HTML I get from the chrome inspector, is this what you meant? About the JS, another user suggested me to turn the JS into an IIFE cose, but I don't know how to do it. – sound wave Commented Sep 12, 2019 at 20:01<p>
tags, which are causing the problem. I copied your code from JSFiddle to a WP install, and without addingparentElement
it worked perfectly, as long as I manually removed the paragraph tags that were automatically added by the WP editor. If those paragraph tags are in there, the.col
and.con
elements are no longer siblings, which is why it breaks. – Steven Commented Sep 12, 2019 at 22:31var content = this.nextElementSibling; if (!content) { content = this.parentElement.nextElementSibling; }
– Steven Commented Sep 12, 2019 at 22:50coll = document.getElementsByClassName("col"); conn = document.getElementsByClassName("con"); var i; for (i = 0; i < coll.length; i++) { coll[i].setAttribute('data-id', 'con' + i); conn[i].setAttribute('id', 'con' + i); coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = document.getElementById(this.getAttribute('data-id')); if (content.style.maxHeight) { content.style.maxHeight = null; } else { content.style.maxHeight = content.scrollHeight + "px"; } }); }
– Steven Commented Sep 13, 2019 at 12:28