如何使这些 javascript 函数普遍适用于任何所需的元素?
在下面的代码中,我想将这 5 个函数减少到 3 个。
第一个函数 toggle_visibility()
已经通过在我从 html 调用该函数时传递 id 来实现通用,但是,我必须重复接下来的两个函数 thankYouText_Change()
和 resettxt()
因为我不知道如何存储 Item
变量的值,也不知道这p
或 OK_button
变量并将它们传递给下一个函数,以便其他函数可以使用它们。
我的目标是弄清楚如何将这些函数减少为一组 3 个函数,这些函数可以随时在我的 html 中访问,并只需使用 onClick="function_foo('desired_element_foo'),而不必每次我想在不同的元素上使用它们时都拥有一组单独的函数,
我认为为了做到这一点,我还需要知道如何创建变量 p 和
OK_Button
的值会根据我发送给它们/访问它们的 ID 自动更改和存储。
function toggle_visibility(id) {
var Item = document.getElementById(id);
if (Item.style.display == 'block') {
Item.style.display = 'none';
}
else {
Item.style.display = 'block';
}
}
function thankYouText_Change() {
var p = document.getElementById("thanksForEmail");
var OK_Button = document.getElementById("okButton");
if (p.innerHTML == 'Thank you for submitting your e-mail.') {
OK_Button.style.display = 'none';
p.innerHTML = "Returning to page...";
setTimeout("toggle_visibility('msgSend'), resettxt()", 500);
}
}
function resettxt() {
var p = document.getElementById("thanksForEmail");
var OK_Button = document.getElementById("okButton");
if (p.innerHTML == 'Returning to page...') {
p.innerHTML = 'Thank you for submitting your e-mail.';
OK_Button.style.display = 'block';
}
}
//Start of repeated functions for second div and button elements
function thankYouText_Change2() {
var p = document.getElementById("thanksForEmail2");
var OK_Button = document.getElementById("okButton2");
if (p.innerHTML == 'Thank you for submitting your e-mail.') {
OK_Button.style.display = 'none';
p.innerHTML = "Returning to page...";
setTimeout("toggle_visibility('msgSend2'), resettxt2()", 500);
}
}
function resettxt2() {
var p = document.getElementById("thanksForEmail2");
var OK_Button = document.getElementById("okButton2");
if (p.innerHTML == 'Returning to page...') {
p.innerHTML = 'Thank you for submitting your e-mail.';
OK_Button.style.display = 'block';
}
}
In the following code I want to reduce these 5 functions down to 3.
The first function toggle_visibility()
is already made universal by passing the id when I call the function from my html, however, I have to repeat the next two functions thankYouText_Change()
and resettxt()
because I don't know how to store the value of the Item
variable, nor the p
or OK_button
variables and pass them to the next function so that they can be used by the other functions.
My goal is to figure out how to reduce these to a set of 3 functions that can be accessed at anytime in my html and applied to any and all relevant elements simply by using onClick="function_foo('desired_element_foo')
, without having to have a separate set of functions for each time I want to use them on a different element.
I think that in order to do this I also need to know how to make the variables p
and OK_Button
have values that will automatically change and be stored based upon the id that I send to them/access them with.
function toggle_visibility(id) {
var Item = document.getElementById(id);
if (Item.style.display == 'block') {
Item.style.display = 'none';
}
else {
Item.style.display = 'block';
}
}
function thankYouText_Change() {
var p = document.getElementById("thanksForEmail");
var OK_Button = document.getElementById("okButton");
if (p.innerHTML == 'Thank you for submitting your e-mail.') {
OK_Button.style.display = 'none';
p.innerHTML = "Returning to page...";
setTimeout("toggle_visibility('msgSend'), resettxt()", 500);
}
}
function resettxt() {
var p = document.getElementById("thanksForEmail");
var OK_Button = document.getElementById("okButton");
if (p.innerHTML == 'Returning to page...') {
p.innerHTML = 'Thank you for submitting your e-mail.';
OK_Button.style.display = 'block';
}
}
//Start of repeated functions for second div and button elements
function thankYouText_Change2() {
var p = document.getElementById("thanksForEmail2");
var OK_Button = document.getElementById("okButton2");
if (p.innerHTML == 'Thank you for submitting your e-mail.') {
OK_Button.style.display = 'none';
p.innerHTML = "Returning to page...";
setTimeout("toggle_visibility('msgSend2'), resettxt2()", 500);
}
}
function resettxt2() {
var p = document.getElementById("thanksForEmail2");
var OK_Button = document.getElementById("okButton2");
if (p.innerHTML == 'Returning to page...') {
p.innerHTML = 'Thank you for submitting your e-mail.';
OK_Button.style.display = 'block';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一遍,您可以将其简化为如下所示:
然后对于页面上的每组元素,您只需使用 3 个相关元素中每一个的正确 ID 调用
thankYouText_Change
即可。对于第二遍,您可以将我的上述两个函数简化为一个,这样您就不需要在相同的元素上多次重新调用
document.getElementById
(并不重要,但我也喜欢用var
声明所有内容 - 变量和函数都一样):(请注意,这消除了对
resettxt
函数的需要。)For a first pass, you could simplify this to something like this:
And then for each set of elements on the page, you just need to call
thankYouText_Change
with the correct IDs for each of the 3 related elements.For a 2nd pass, you could simplify both of my above functions into one, so that you don't need to re-call
document.getElementById
on the same elements more than once (not significant, but I also like to declare everything withvar
- variables and functions alike):(Note that this eliminates the need for a
resettxt
function.)