[英]Assigning value of data- attribute to seperate html element
I have been trying to build a popup menu using HTML, CSS, jQuery.我一直在尝试使用 HTML、CSS、jQuery 构建弹出菜单。 I obviously don't want to have to copy blocks of code for every menu item and am using (this) with the data-*
attribute to do so.我显然不想为每个菜单项复制代码块,而是使用带有data-*
属性的 (this) 来执行此操作。
My relevant code is:我的相关代码是:
$(".source_dropdown_button").click(function() { var src_content = $(this).data("content"); $(src_content).style.display = "block"; });
<p><button class="source_dropdown_button" id="ds_dropdown_button" data-content="#ds_dropdown_content">Sources: Domestic</button></p> <div class="source_dropdown_content" id="ds_dropdown_content"> <a href="">External/internal link 1</a> <a href="">External/internal link 2</a> <a href="">External/internal link 3</a> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
When I run it like this the console says that it can't assign 'display' to undefined so I replaced the lasts line with - console.log(src_content)
当我这样运行它时,控制台说它不能将“显示”分配给未定义,所以我用console.log(src_content)
替换了最后一行
The console now returns the string "#ds_dropdown_content"
which is the value I gave the data- attribute in my HTML while actually trying to assign the DIV with id="ds_dropdown_content"
to the attribute value.控制台现在返回字符串"#ds_dropdown_content"
,这是我在 HTML 中赋予data-属性的值,同时实际尝试将id="ds_dropdown_content"
的 DIV 分配给属性值。
So I'm pretty sure I found my problem, I'm just short on solutions and haven't been able to find a successful answer on the internet.所以我很确定我找到了我的问题,我只是缺乏解决方案,并且无法在互联网上找到成功的答案。
Can someone please tell me: how I can assign the contents of this element to the value of my data- attribute?有人可以告诉我:如何将此元素的内容分配给我的 data- 属性的值? Thank you谢谢
That's because $(src_content)
is a jQuery object, and it does not expose the .style
property like JS does for HTMLElement.这是因为$(src_content)
是一个 jQuery object,它不会像 JS 对 HTMLElement 那样暴露.style
属性。
Use jQuery's.css() Method:使用 jQuery 的.css() 方法:
$(src_content).css({display: "block"});
or extract first the Element using [0]
or jQuery's .get(0)
before using JS's .style
或在使用 JS 的.style
之前先使用[0]
或 jQuery 的.get(0)
提取元素
$(src_content)[0].style.display = "block";
https://developer.mozilla.org/en-US/docs/Web/API/ElementCSSInlineStyle/style https://developer.mozilla.org/en-US/docs/Web/API/ElementCSSInlineStyle/style
Also always use type="button"
for <button>
.也总是使用type="button"
作为<button>
。 Otherwise one day you'll start to submit forms;)否则有一天你会开始提交 forms;)
A better suggestion would be to handle the styles exclusively via CSS stylesheet,更好的建议是通过 CSS 样式表专门处理 styles,
and use JS's Element.classList.toggle()
or jQuery's $().toggleClass()
to toggle that one specific class:并使用 JS 的Element.classList.toggle()
或 jQuery 的$().toggleClass()
来切换一个特定的 class:
$("[data-content]").on("click", function() { $(this.dataset.content).toggleClass("is-hidden"); });
/* bool helpers */.is-hidden { display: none; }
<p> <button type="button" class="source_dropdown_button" id="ds_dropdown_button" data-content="#ds_dropdown_content">Sources: Domestic</button> </p> <div class="is-hidden" id="ds_dropdown_content"> <a href="">External/internal link 1</a> <a href="">External/internal link 2</a> <a href="">External/internal link 3</a> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.