简体   繁体   English

无法在 perticulat blogspot 执行 javascript,但它在其他 blogspot 中工作

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

I have successfully executed a JavaScript on a blog .我已经成功地在博客上执行了 JavaScript。 But, I am unable to execute JavaScript at a particular blogspot site.但是,我无法在特定的 blogspot 站点上执行 JavaScript。 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?博客上的任何 JS 都停止执行了吗? Any CSS issues?任何CSS问题? 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:对于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我在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?) (或者找出为什么找不到兄弟,可能是 HTML 结构问题?)

I have duplicated this issue in a CodePen: https://codepen.io/Alexander9111/pen/vYOBero我在 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真正的错误是网站上缺少此<div id="bra-results"></div> 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.您的函数function find_sibling(input)没有返回您期望的节点。

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 :)并像我说的那样在表单之后添加 HTML: <div id="bra-results"></div> ,然后一切都应该工作:)

See my CodePen for full demo and full code: https://codepen.io/Alexander9111/pen/vYOBero有关完整演示和完整代码,请参阅我的 CodePen: https ://codepen.io/Alexander9111/pen/vYOBero

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

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