简体   繁体   English

单击输入框时,需要显示我的段落。 我的p数组如何不循环遍历我的段落变量?

[英]I need my paragraph to display when clicking on my input boxes. How is my p array not cycling through my paragraph variables?

I am trying to loop through my p array to find each paragraph variable so that I can change the opacity of my HTML. 我试图遍历我的p数组以找到每个段落变量,以便可以更改HTML的不透明度。

var p1 = document.querySelector(".p1");
var p2 = document.querySelector(".p2");
var p3 = document.querySelector(".p3");

var p = [1,2,3];
function visible() {
    for (var i = 0; i < p.length; i++) {
        p[i].style.opacity = 1;
    }

 }

The result I am looking for is when I click an input text box I want my paragraph to show. 我要查找的结果是当我单击要显示段落的输入文本框时。 The visible function is an html onclick function. 可见函数是html onclick函数。

I think you mean 我想你是说

var p = [p1,p2,p3];

not

var p = [1,2,3];

edited 已编辑

You can add a parameter inside your html. 您可以在html中添加参数。

<input type="number" onclick="displayParagraph('.p1')">
<input type="number" onclick="displayParagraph('.p2')">
<input type="number" onclick="displayParagraph('.p3')">
function displayParagraph(paragraphClass){
var p = document.querySelector(paragraphClass);
p.style.opacity = 1;
}

 let p1 = document.getElementsByClassName("p1"); let p2 = document.getElementsByClassName("p2"); let p3 = document.getElementsByClassName("p3"); let pArray = []; pArray.push(p1,p2,p3); setTimeout(()=>{ pArray.map((x)=>{ x[0].style.opacity =1 }) },0); 

Now we have to use the setTimeout function, because the p1 and p2 and p3 are Still unknown to the DOM in your javascript file. 现在,我们必须使用setTimeout函数,因为p1和p2和p3对于您的JavaScript文件中的DOM仍然是未知的。 if you will use JQuery for instance and put your code in the $( document ).ready() {} function it will be known. 例如,如果您将使用JQuery并将代码放入$(document).ready(){}函数中,它将是已知的。

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

相关问题 为什么我的类 Paragraph 没有制作段落元素<p>一种</p>在 JavaScript 中? - Why is my class Paragraph not making a paragraph element <p>a</p> in JavaScript? 如何在段落中显示我的按钮的值 - How to display the value of my button in a paragraph 当用户单击时,如何停止文本在数组中循环显示? - How do I stop my text from cycling through the array when the user clicks? 我如何修复我的代码以将每个段落放在一个新行中,因为我试图在给定段落之前添加段落 - How can i fix my code to put each paragraph in a new line, as I am trying to add paragraph before the given paragraph 将内容放入“弹出”框中。 HTML CSS和JS - Put content into my “popup” boxes. HTML CSS & JS 如何创建一个函数来将我的段落更改为带有onmouseover的img - How do I create a function to change my paragraph into an img with an onmouseover 我如何在 html 标签(如段落)中使用 if 语句显示 for 循环的结果 - How do i display the outcome of my for loop with if statement in an html tag like a paragraph 如何在段落中添加行号? - How can I add line numbering to my paragraph? 在InDesign中选择后,如何获得下一段? - How can I get the next paragraph after my selection in InDesign? 如果在textarea中插入段落,如何设置div的其他位置 - How to set a different position of a div if I insert a paragraph in my textarea
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM