简体   繁体   English

JavaScript 内部不工作 HTML Ajax 已加载内容

[英]JavaScript not working inside HTML Ajax loaded content

I am having an issue when trying to run JavaScript inside an Ajax loaded content on a webpage.尝试在网页上的 Ajax 加载内容中运行 JavaScript 时遇到问题。 I keep getting an error "Cannot set properties of null".我一直收到错误消息“无法设置 null 的属性”。 If I move the output of the script to the footer for example which is outside of the Ajax loaded content, the script works fine.例如,如果我将脚本的 output 移动到页脚,它位于 Ajax 加载的内容之外,脚本可以正常工作。

This is the piece of code where the ajax is loaded into on the main webpage:这是在主网页上加载 ajax 的一段代码:

<main>
    <article class="ext-content">

    </article>
    </main>

This is the JavaScript code to load the ajax content:这是加载 ajax 内容的 JavaScript 代码:

$('section li').click(function() {
    $.ajax({
      type: 'GET',
      url: 'includes/ext-content/'+$(this).data('content')+'.html',
      dataType: 'html',
      success: function(response) {
        $('.ext-content').html(response);
      }
    });
  });

This is part of the ajax loaded content where when a user selects the amount of items, they are shown the price of the item * the amount selected.这是 ajax 加载内容的一部分,当用户选择商品数量时,会显示商品价格 * 所选数量。 The shopOutput is where the price will be shown on the webpage: (If I move the shopOutput outside of the Ajax content the script works fine.) shopOutput是价格将在网页上显示的位置:(如果我将shopOutput移到 Ajax 内容之外,脚本工作正常。)

          <tr>
            <td colspan="3" class="shop-qty-select">
                <select onchange="addElements(this)">
                  <option value="0">Select aantal:</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
                  <option value="6">6</option>
                  <option value="7">7</option>
                  <option value="8">8</option>
                  <option value="9">9</option>
                  <option value="10">10</option>
                </select>
                <p id="shopOutput"></p>
            </td>
        </tr>

This is the JavaScript which takes the amount selected and shows the user how much the total price will be:这是 JavaScript,它采用所选的金额并向用户显示总价是多少:

let shopOutput = document.getElementById("shopOutput");

const priceFractal = 10;

function addElements(selectElement) {

  let valueNumbers = selectElement.value;

  shopOutput.innerHTML = ""; 

    let yourTotal = valueNumbers * priceFractal;
    shopOutput.textContent = yourTotal;
}

Any help is much appreciated.任何帮助深表感谢。

Like I mentioned I know that this code works if I just move the output outside of the Ajax loaded content.就像我提到的那样,我知道如果我只是将 output 移到 Ajax 加载的内容之外,这段代码就可以工作。 But when it is inside the Ajax content, I get an error "Cannot set properties of null (setting 'innerHTML')"但是当它位于 Ajax 内容中时,我收到错误消息“无法设置 null 的属性(设置‘innerHTML’)”

This is running immediately when the page loads:这在页面加载时立即运行:

let shopOutput = document.getElementById("shopOutput");

Since that #shopOutput element doesn't exist yet, the shopOutput variable will of course be null .由于该#shopOutput元素尚不存在,因此shopOutput变量当然null And it will always continue to be null since it's never re-assigned.而且它将始终继续为null ,因为它从未重新分配过。

If it doesn't need to be used until the function is invoked, set it in the function instead:如果在调用 function 之前不需要使用它,请将其设置为 function:

function addElements(selectElement) {
  let shopOutput = document.getElementById("shopOutput");
  // the rest of the function...
}

Anything that deals with a reference to an element should be contained in a $.ready() so that the browser waits before attempting to get references to things that don't exist yet.任何处理对元素的引用的东西都应该包含在 $.ready() 中,以便浏览器在尝试获取对尚不存在的事物的引用之前等待。

$( document ).ready(function() {
    //your code here
});

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

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