简体   繁体   中英

Unable to execute javascript at a perticulat blogspot, but it work in other blogspot

I have successfully executed a JavaScript on a blog . But, I am unable to execute JavaScript at a particular blogspot site. Please have a look at bra calculator . Why it does not work at this spa site? Is any JS on the blog stopping to execute? Any CSS issues? Please help me to figure out this issues.

I do not get any error messages.

<form id="bra-calculator">
  <div class="title">
Enter measurements in inches</div>
<div class="half">
    <label for="rib-measurement">Enter rib measurement:</label>
    <input id="rib-measurement" name="rib-measurement" placeholder="Rib Measurement" type="number" />
    <div class="error-message">
Please enter a value greater than or equal to 24</div>
</div>
<div class="half">
    <label for="bust-measurement">Enter bust measurement:</label>
    <input id="bust-measurement" name="bust-measurement" placeholder="Cup Measurement" type="number" />
    <div class="error-message">
Your bust measurement must be at least 1 inch larger than rib</div>
</div>
<input id="submit" type="submit" value="Calculate!" />
</form>
<div id="bra-results">
</div>

For JS:

var bra_calculator = bra_calculator || (function() {

  var inputs = document.getElementsByTagName('input'),
    ribs_input = document.getElementById('rib-measurement'),
    bust_input = document.getElementById('bust-measurement'),
    validation_flag = false,
    results_div = document.getElementById('bra-results');

  function results_message(message) {
    results_div.innerHTML = message;
  }

  function find_sibling(input) {
    return input.nextSibling.nextSibling;
  }

  function add_error(input) {
    input.classList.add('error');
    find_sibling(input).classList.add('show');
  }

  function remove_error(input) {
    input.classList.remove('error');
    find_sibling(input).classList.remove('show');
  }

  function error_checker(input) {
    if (input.value == 0) {
      add_error(input);
    } else {
      if (input.type.toLowerCase() === 'number') {
        remove_error(input);
      }
    }
  }

  function validator() {
    // check for empty values
    for (i = 0; i < inputs.length; ++i) {
      error_checker(inputs[i]);
    }

    // rib measurement should be be >= 24
    if (ribs_input.value < 24) {
      add_error(ribs_input);
    } else {
      remove_error(ribs_input);
    }

    // bust should be at least 1 inch greater than ribs!
    if (bust_input.value !== '' && ribs_input.value !== '') {
      if (Number(bust_input.value) <= Number(ribs_input.value)) {
        add_error(bust_input);
      } else {
        remove_error(bust_input);
      }
    }

    if (!ribs_input.classList.contains('error') && !bust_input.classList.contains('error')) {
      validation_flag = true;
    } else {
      validation_flag = false;
      results_message('');
    }

    if (validation_flag) {
      var cup_sizes = ['A', 'B', 'C', 'D', 'DD', 'E', 'F', 'FF', 'G', 'GG', 'H', 'HH', 'J', 'JJ', 'K', 'KK', 'L', 'LL', 'M', 

'MM', 'N', 'O', 'OO'],
        bust = Math.ceil(bust_input.value),
        ribs = Math.ceil(ribs_input.value),
        difference = (bust - ribs) - 1;

      if (cup_sizes[difference] === null) {
        results_message('Your size is out-of-range! Please try again.');
      } else {
        results_message('Your calculated size is: <span>' + ribs + cup_sizes[difference] + '</span>');
      }
    }
  }

  return {
    validator: validator
  }
})();

(function() {
  document.getElementById('bra-calculator').addEventListener('submit', function(e) {
    e.preventDefault();
    bra_calculator.validator();
  });
})();

My question is why it work in one and not in another? Please click both links and see the differences.

I see the following errors on https://srilanka-massage.blogspot.com/p/bra-size-calculator.html

在此处输入图片说明

在此处输入图片说明

this tells me that we need to modify:

function remove_error(input) {
    input.classList.remove('error');
    let sibling = find_sibling(input);
    if (sibling){
        sibling.classList.remove('show');
    }
  }

Or another similar error handling. (Or find out why the sibling is not found, likely a HTML structure issue?)

I have duplicated this issue in a CodePen: https://codepen.io/Alexander9111/pen/vYOBero

The real error is that this <div id="bra-results"></div> is missing on the site https://srilanka-massage.blogspot.com/p/bra-size-calculator.html

When I added this tag after the form, everything works:

在此处输入图片说明

However, your code is still producing other errors.

Your function function find_sibling(input) , is not returning the node you expect.

You should change it to:

  function find_sibling(input) {
    if (input.id.includes('measurement')){
      return input.parentNode.querySelector('div');
    } else {
      return input.nextSibling.nextSibling;
    }    
  }

You should also change:

  function add_error(input) {
    input.classList.add('error');
    let sibling = find_sibling(input);
      if (sibling.classList){
          find_sibling(input).classList.add('show');
      }    
  }

And add the HTML: <div id="bra-results"></div> after the form like I said, then all should work :)

See my CodePen for full demo and full code: https://codepen.io/Alexander9111/pen/vYOBero

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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