Я делаю сокращенную функцию для построения функции JavaScript:
function editById(getId) {
document.getElementById(getId);
}
то я попытался использовать его в моем script.js:
function run() {
document.editById("test").style.color="blue";
}
то мой HTML:
<p id="test">TestTestestestse</p>
<input type="button" onclick="run()" value="Change text color">
Это не работает с editById
, но это getElementById
...
ABOVE SOLVED
Добавляем к этому:
Теперь это не работает:
shortHand.js
// Long term document functions
document.editById = function (find) { return document.getElementById(find); }
document.editByClass = function (find2) { return document.getElementsByClassName(find2); }
document.editByTag = function (find3) { return document.getElementsByTagName(find3); }
script.js
function run() {
document.editById("test").style.color="blue";
}
function run2() {
document.editByClass("test").style.color="blue";
}
function run3() {
document.editByTag("h1").style.color="blue";
}
HTML
<p id="test">TestTestestestse</p>
<p class="test">TestTestestestse222</p>
<h1>TestTestestestse368585475</h1>
<input type="button" onclick="run()" value="Change text color">
<input type="button" onclick="run2()" value="Change text color">
<input type="button" onclick="run3()" value="Change text color">
Измените свою функцию как
function editById(getId) {
//return object
return document.getElementById(getId);
}
function run() {
editById("test").style.color="blue";
}
Кроме того, вы также используете
document.editById = function (getId) {
return document.getElementById(getId);
}
function run() {
document.editById("test").style.color = "blue";
}
var editById = document.getElementById.bind(document);
, var editById = document.getElementById.bind(document);
функция editById зарегистрирована в документе Object, но в окне. вы не можете вызвать функцию, подобную этой
вы можете вызвать функцию, подобную этой
function run() {
editById("test").style.color="blue";
}
return
document.editById
?