mercredi 30 août 2017

Converting a with statement to a togglable if

I am trying to make a bookmarklet that I can use to quickly toggle the class/id of elements. I found this [old] code for toggling highlighting links. I have read that the with statement should be avoided unless you are using use strict;. I couldn't find any walk through on how with would look like if, and not sure how it would look

javascript: for (nIx5Bs = 0; nIx5Bs < document.all.length; nIx5Bs++) {
  if (document.all[nIx5Bs].tagName == 'A') {
    with(document.all[nIx5Bs].style) {
      if (backgroundColor == 'yellow') {
        void(backgroundColor = document.bgColor)
      } else {
        void(backgroundColor = 'yellow')
      }
    }
  }
}

I found another bookmarklet to show ids, but it isn't togglable.

javascript: var t = document.getElementsByTagName('div');
for (i = 0; i < t.length; i++) {
  void(t[i].style.padding = '5px;');
  b = t[i].className;
  h = t[i].innerHTML;
  void(t[i].innerHTML = '<p style=\'color:red;font-weight:bold;\'>' + b + '</p>' + h);
}

Any hints to make this togglable would be great.

Aucun commentaire:

Enregistrer un commentaire