简体   繁体   English

基本 HTML JavaScript function 未按预期工作

[英]Basic HTML JavaScript function not working as intended

I'm wandering if anyone can help.如果有人可以提供帮助,我正在徘徊。 Im doing some rather basic JS for a project but I'm an idiot and not sure how to do this element.我为一个项目做了一些相当基本的 JS,但我是个白痴,不知道如何做这个元素。

I've used an opensource word highlighting program i've found online which works when i input a string to be highlighted, but when i input "input_id.value" which is a working (tested) value of an input box, The highlighter doesnt work.我使用了我在网上找到的开源单词突出显示程序,当我输入要突出显示的字符串时,它可以工作,但是当我输入“input_id.value”这是输入框的工作(测试)值时,荧光笔没有工作。 I thought it could be that it's not updating ie it only runs once so it will try to highlight no value as nothing has been inputted.我认为可能是它没有更新,即它只运行一次,所以它会尝试突出显示没有值,因为没有输入任何内容。

This is the code snippet:这是代码片段:

<script type="text/javascript" src="../Resources/hilitor.js"></script>
<script type="text/javascript">

var input = document.getElementById("input_id").value;

    // global variable
    var myHilitor;
    document.addEventListener("DOMContentLoaded", function() {
        myHilitor = new Hilitor();
        myHilitor.apply("fox");
    }, false);


</script>

This works correctly and higlights "fox" as shown in this image.这可以正常工作并突出显示“狐狸”,如图所示。 狐狸

However when i change但是当我改变

myHilitor.apply("fox");

to

myHilitor.apply(input_id.value);

nothing is highlighted at all.根本没有突出显示。 I tried putting the whole thing into a function like an idiot but that also doesnt work我试着像一个白痴一样把整个东西放进 function 但这也行不通

<script type="text/javascript" src="../Resources/hilitor.js"></script>
<script type="text/javascript">
function searchFunction(){
    var input = document.getElementById("input_id").value;

    // global variable
    var myHilitor;
    document.addEventListener("DOMContentLoaded", function() {
        myHilitor = new Hilitor();
        myHilitor.apply("fox");
    }, false);
}

</script>

The function is called by function 由

<button onclick="searchFunction()" class="button"></button>

I'm truly dumbfounded, any help would be very appreciated.我真的傻眼了,任何帮助将不胜感激。 Thankyou:)谢谢:)

Thanks all for the helpful contributions, Problem is solved I'm waiting to be able to accept answer感谢大家的帮助,问题已解决,我正在等待能够接受答案

document.addEventListener("DOMContentLoaded", function() { causes a function to run when the DOMContentLoaded event happens (which is about when </html> is parsed as the document loads). document.addEventListener("DOMContentLoaded", function() {导致 function 在 DOMContentLoaded 事件发生时运行(大约在</html>被解析为文档加载时)。

You haven't shown how searchFunction is called, but odds are that it is when a button is clicked.您还没有展示searchFunction是如何被调用的,但很可能是在单击按钮时。

That will happen after the DOMContentLoaded event has occurred so the condition for the function running never happens.这将在 DOMContentLoaded 事件发生发生,因此 function 运行的条件永远不会发生。

Don't include that condition.不要包括那个条件。

Your variable is called var input but you're using input_id here myHilitor.apply(input_id.value) ;您的变量称为var input但您在此处使用input_id myHilitor.apply(input_id.value)

You have declared var input = document.getElementById("input_id").value;您已声明var input = document.getElementById("input_id").value; . .

But while calling myHilitor.apply(input_id.value);但是在调用myHilitor.apply(input_id.value); you are passing input_id.value .您正在传递input_id.value You should pass just input .你应该只通过input

Like myHilitor.apply(input);myHilitor.apply(input);

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

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