简体   繁体   English

代码会更新颜色,但不会更新实际文本吗?

[英]Code updates color but not the actual text?

This chunk of code that I have updates the color of the text but not the actual text... 我拥有的这段代码会更新文本的颜色,但不会更新实际的文本...

var lastWordTyped;
var ele = document.querySelector("#my_text");
//code
//...
lastWordTyped = capitalizeFirstLetterOfKeyword(lastWordTyped);
lastWordTyped = lastWordTyped.fontcolor("blue");
ele.innerHTML = lastWordTyped;


function capitalizeFirstLetterOfKeyword(keyword) {

        var word;
        word = keyword.charAt(0).toUpperCase() + keyword.slice(1);
        return word;
    }

When I step through it, is recognizing what the new string should be but it doesnt update the actual text to the new string, but it does change its color. 当我逐步执行此操作时,正在识别新字符串应该是什么,但是它不会将实际文本更新为新字符串,但是会更改其颜色。 Can anybody provide me with the reason as to why it won't update it? 谁能为我提供不更新的原因?

You haven't declared lastWordTyped, so the capitalizeFirstLetterOfKeyword() gets an 'undefined' as input. 您尚未声明lastWordTyped,因此capitalizeFirstLetterOfKeyword()获得了“未定义”作为输入。

Working demo here: http://jsfiddle.net/yc4mvm1n/1/ 此处的工作示例: http : //jsfiddle.net/yc4mvm1n/1/

var lastWordTyped = "NewWord";
var ele = document.querySelector("#my_text");

lastWordTyped = capitalizeFirstLetterOfKeyword(lastWordTyped);
lastWordTyped = lastWordTyped.fontcolor("blue");
ele.innerHTML = lastWordTyped;

function capitalizeFirstLetterOfKeyword(keyword) {
        var word;
        word = keyword.charAt(0).toUpperCase() + keyword.slice(1);
        return word;
    }

Your code looks fine to me. 您的代码对我来说很好。

http://plnkr.co/edit/mRllGWD2bNTxxyWrJENZ?p=preview http://plnkr.co/edit/mRllGWD2bNTxxyWrJENZ?p=preview

Try posting some more code. 尝试发布更多代码。

function capitalizeFirstLetterOfKeyword(keyword) {

        var word;
        word = keyword.charAt(0).toUpperCase() + keyword.slice(1);
        return word;
    }

is returning proper capitalized word back, I am assuming that you are setting values to lastWordTyped somewhere in your //code section 返回正确的大写单词,我假设您正在//code部分中的某个位置为lastWordTyped设置值

Make sure the element #my_text exists before the JS code is run. 在运行JS代码之前,请确保元素#my_text存在。 You can do this by putting the script at the bottom of the body, or by using 您可以通过将脚本放在正文底部或使用

window.onload = function() {
    // your code here
}

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

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