改变JavaScript中的选项下拉式菜单以更改`div`的样式。

huangapple go评论56阅读模式
英文:

change stye div by option drop down in JavaScript

问题

I tried to do this code HTML/CSS/JavaScript

Have idea Div and options (01-02-03)

When i select option 01 will give me style css ex; shadow, than select option 02 will give me other style shadow

<div id="card-shadow-control" style="width: 100px; border: 1px solid black; margin: auto; padding: 20px; text-align: center">Hello</div>
<select id="choose">
  <option value="01" style="box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);">shadow01</option>
  <option value="02">shadow02</option>
  <option value="03" style="box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);">shadow03</option>
</select>

<script>
function myFunction() {
  var x = document.getElementById("choose").options[0].text;
  document.getElementById("card-shadow-control").innerHTML = x;
}
</script>

I tried to do that but it doesn't work.

英文:

I tried to do this code HTML/CSS/JavaScript

Have idea Div and options (01-02-03)

When i select option 01 will give me style css ex; shadow, than select option 02 will give me other style shadow

‹div id= card-shadow-control&quot; style=&quot;width: 100px; border: 1px solid black; margin: auto;
padding: zOpx; text-align: center &gt;Hello‹/div&gt;
‹select id=&quot;choose!&gt;
‹option value=&quot;ol&quot; style= box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb (0 0 0 / 0.1) ; &quot;›shadow01‹/option&gt; option value=&quot;02&quot;›shadow02‹/option)
&lt;option value=&quot;03&quot; style=&quot;box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.1, 0 8px 10px
- 6px
reb (0 0 0 / 0.1);&quot;&gt;shadow03&lt;/option) &lt;/select&gt;

&lt;script&gt;
function myFunction () {
var x = document. getElementById (&quot;choose&quot;) .options [0] .text;
document. getElementById(&quot;card-shadow-control&quot;).innerHTML=x;} 
&lt;/script&gt;

I tried to do that but not work

答案1

得分: 0

要更改元素中的 CSS 阴影效果,您应该在 .onchange 事件中执行。获取选项的值并将其用作选择器,以获取所选选项元素上的 data-box-shadow。然后,在 card-shadow-control 中应用它作为阴影样式。确保您的代码如下所示:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
document.getElementById("choose").onchange = function() {
  var x = document.getElementById("choose").value;
  var option = document.querySelector(`option[value="${x}"]`);
  if (option.dataset.boxShadow) {
    document.getElementById("card-shadow-control").style.boxShadow = option.dataset.boxShadow;
  } else {
    document.getElementById("card-shadow-control").style.boxShadow = '';
  }
}

<!-- language: lang-html -->
<div id="card-shadow-control" style="width: 100px; border: 1px solid black; margin: auto; padding: zOpx; text-align: center">Hello</div>

<select id="choose">
  <option value="01" data-box-shadow="0 4px 6px -1px rgba(0, 0, 0, 1), 0 2px 4px -2px rgba(0, 0, 0, 1)">shadow01</option>
  <option value="02" selected>shadow02</option>
  <option value="03" data-box-shadow="0 20px 25px -5px rgba(0, 0, 0, 1), 0 8px 10px -6px rgba(0, 0, 0, 0.1)">shadow03</option>
</select>

<!-- end snippet -->

以上是代码部分,已翻译完毕。

英文:

To change CSS Box Shadow in the element, you should do that in the .onchange event. Take the value of the option and use it as a selector to get the data-box-shadow on the selected option element. Then, apply it in box-shadow style at card-shadow-control. Make sure your code looks like this :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.getElementById(&quot;choose&quot;).onchange = function() {
  var x = document.getElementById(&quot;choose&quot;).value;
  var option = document.querySelector(`option[value=&quot;${x}&quot;]`);
  if(option.dataset.boxShadow) {
    document.getElementById(&quot;card-shadow-control&quot;).style.boxShadow = option.dataset.boxShadow;
  } else {
    document.getElementById(&quot;card-shadow-control&quot;).style.boxShadow = &#39;&#39;;
  }
  
}

<!-- language: lang-html -->

&lt;div id=&quot;card-shadow-control&quot; style=&quot;width: 100px; border: 1px solid black; margin: auto;
padding: zOpx; text-align: center&quot;&gt;Hello&lt;/div&gt;

&lt;select id=&quot;choose&quot;&gt;
  &lt;option value=&quot;01&quot; data-box-shadow=&quot;0 4px 6px -1px rgba(0, 0, 0, 1), 0 2px 4px -2px rgba(0, 0, 0, 1)&quot;&gt;shadow01&lt;/option&gt;
  &lt;option value=&quot;02&quot; selected&gt;shadow02&lt;/option&gt;
  &lt;option value=&quot;03&quot; data-box-shadow=&quot;0 20px 25px -5px rgba(0, 0, 0, 1), 0 8px 10px -6px rgba(0, 0, 0, 0.1)&quot;&gt;shadow03&lt;/option&gt; 
&lt;/select&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 13:17:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定