简体   繁体   English

调用一个值 + style.display 函数?

[英]Calling a value + style.display function?

I'm struggling to get this working because I don't know the right formatting.我正在努力让它工作,因为我不知道正确的格式。

What I am attempting is to get a CSS modal to display depending on what a user selects as a value in a Javascript applet.我正在尝试的是根据用户在 Javascript 小程序中选择的值来显示 CSS 模式。

The idea is to return .style.display = "block";这个想法是返回 .style.display = "block";

function onClick(event){
    <something>.style.display = "block"; 
}

Where contains a value that has being saved in the format of intersects[0].object.title其中包含以 intersects[0].object.title 格式保存的值

So if for example I have selected "manhattan"因此,例如,如果我选择了“曼哈顿”

alert(intersects[0].object.title)

I'll get the string "manhattan" displaying correctly.我会正确显示字符串“manhattan”。 That works perfectly.这完美地工作。

But I can't get manhattan.style.display = "block";但我无法得到 manhattan.style.display = "block"; returned and WORKING inside the function?返回并在函数内工作? I tried :我试过 :

function onClick(event){
    intersects[0].object.title.style.display = "block"; 
}

Also tried也试过

function onClick(event){
    (intersects[0].object.title).style.display = "block"; 
}

Any help would be greatly appreciated任何帮助将不胜感激

This may not be directly what you're looking for, but it may help anyways.这可能不是您正在寻找的直接内容,但无论如何它可能会有所帮助。 To make it work in your case, just change the button press to be a check for the selected value.要使其适用于您的情况,只需将按钮更改为检查所选值即可。

Rather than adjusting the CSS directly, this route modifies the element's classList to remove or add a .hidden class that contains the correct CSS.而不是直接调整CSS,这条路线修改元素的classList删除或添加.hidden包含正确的CSS类。

 // Loop through all modal buttons document.querySelectorAll('.modal-button').forEach(function(element) { // Add a click event listener to all modal buttons element.addEventListener('click', function() { // Toggle the target modal on click toggleModal(element.dataset.target); }); }); // Create the function to toggle the modals function toggleModal(target) { // Find the target let targetElement = document.querySelector(target); // Check if the target is hidden if (targetElement.classList.contains('hidden')) { // If it is, show it targetElement.classList.remove('hidden'); } else { // If it isn't, hide it targetElement.classList.add('hidden'); } }
 .hidden { display: none; }
 <button data-target="#modal" class="modal-button">Toggle Modal</button> <div id="modal" class="hidden">Hey there, I'm a modal!</div>

I'm not certain from your question how the pieces of your puzzle are related to one another, and it would be helpful if you could clarify by showing more of your HTML and Javascript code, but I'll toss a couple of ideas at you in the meantime.从你的问题中我不确定你的拼图是如何相互关联的,如果你能通过展示更多的 HTML 和 Javascript 代码来澄清会很有帮助,但我会向你提出一些想法同时。 Apologies if I'm telling you stuff you already know.如果我告诉你你已经知道的东西,我深表歉意。

The only sort of object you would usually be able to set "style.display" on is an HTML element.通常可以设置“style.display”的唯一对象是 HTML 元素。 To tell Javascript which HTML element you want to modify, you usually use a CSS selector like "document.getElementById('myDiv')"要告诉 Javascript 您要修改哪个 HTML 元素,您通常使用 CSS 选择器,如“document.getElementById('myDiv')”

It sounds like "manhattan" might be a piece of information you could use to uniquely identify the HTML element you intend to show.听起来“曼哈顿”可能是一条信息,您可以使用它来唯一标识要显示的 HTML 元素。 If so, there are four simple parts to showing the correct element:如果是这样,有四个简单的部分可以显示正确的元素:

  • associate the element with that particular string (eg via an id)将元素与该特定字符串相关联(例如,通过 id)
  • get the string at runtime (the same way as you did for the alert)在运行时获取字符串(与您对警报所做的方式相同)
  • select the element based on the matching string根据匹配的字符串选择元素
  • display the selected element显示选中的元素

All together, it might look like this:总之,它可能看起来像这样:

<div id="manhattan"></div>
<script>
var identifier = intersects[0].object.title;
alert(identifier) //I'm assuming this alerts "manhattan"
var elementToShow = document.getElementById(identifier);
elementToShow.style.display = "block";
</script>

Is this on the right track?这是在正确的轨道上吗? If not, just provide more detail, and I'll see what else I can suggest.如果没有,请提供更多详细信息,我会看看我还能提出什么建议。

Give to you div some id and then just change that:给你 div 一些 id 然后改变它:

<div id="test"></div>

document.getElementById("test").style["display"] = "block";
or
document.getElementById("test").style.display = "block";
or 
document.getElementById("test").style.setProperty('display', 'block');
or
document.getElementById("test").setAttribute('display', 'block');

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

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