问题描述:

I'm running drupal 8 and I have a content type with the following fields

  • Image
  • background color scheme
  • title

I have view with 4 grids

now what i want to do is get the value from the background color scheme field and apply it as background color...

<div class=grid1> </div>

<div class=grid2> </div>

<div class=grid3> </div>

<div class=grid4> </div>

is there a way to do this using javascript .... each div will have diff background color.

网友答案:

Lets consider the input field for the background color scheme. You can retrieve the value of the input field using the javascript value method.

<div class="grid">    </div>
<div class="grid">    </div>
<div class="grid">    </div>
<div class="grid">    </div>

<input class="scheme" type="text" />
<input class="scheme" type="text" />
<input class="scheme" type="text" />
<input class="scheme" type="text" />

<script type="text/javascript">
  var grids = document.getElementsByClassName('grid');
  var colorinput = document.getElementsByClassName('scheme');
  var i;
  for (i = 0; i < colors.length; i++) {
    grids[i].style.background = colorinput[i].value;
  }
</script>

Make sure to place the script after the grids, to avoid invalid reference errors.

相关阅读:
Top