- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是对我昨天的问题的跟进(Jquery Create "array"of .click functions")。)
昨天,我想我会把我问的简化一下,希望我能举一反三。我做不到,还有一些事情我想不通。
所以这个新问题,拜托,我承认我是一个新手,我的问题对你们中的一些人来说可能看起来太愚蠢了,请耐心等待我!
我有一组双 div:
<div id="div_a_1" class="pingpongdiv">hkh</div><div id="div_b_1" class="pingpongdiv">nuigyuig</div>
<div id="div_a_2" class="pingpongdiv">uynyn</div><div id="div_b_2" class="pingpongdiv">,jomoi</div>
<div id="div_a_3" class="pingpongdiv">yfvrcers</div><div id="div_b_3" class="pingpongdiv">rdcsers</div>
目前上面的代码是手写的,以后打算根据自己需要的div组数用Jquery生成。
他们得到了以下 css:
.pingpongdiv { /* make divs nearly invisible */
opacity: .15;
}
我想通过它们实现以下目标:
当我点击“a”或“b”div 时,它的不透明度通过动画从 0.15 变为 1(变得可见)。不过有一个问题:在我开始“激活”相应的“b”div 之前,我必须至少单击“a”div 一次!
我有以下 jquery 代码(请记住,我是新手,我可能会使用非常繁琐的方法,但它可能更优雅,请帮助我):
// define the array of flags used to check whether an "a" div was clicked on.
var leftPictureSeen = [];
leftPictureSeen[0] = 0; // never used because divs names start at 1
for (var i = 1; ; i++) {
if ($('#div_a_' + i.toString()).length) // If this div exists, add one more element to the array
leftPictureSeen[i] = 0;
else break;
}
// Attach the function that are needed for the divs to be seen and "unseen".
// ATTENTION: Here I mix what I learned yesterday about using "this"
// and I still have an "i" in the function definition, although I know that "i" is out of scope there.
// The idea is that I would like to know how I manage this issue properly so that it works
for (var i = 1; i < leftPictureSeen.length; i++)
{
$("#div_a_" + i.toString()).click(function(){
$(this).animate({opacity:1}, 1000);
// How do I replace the line below so that it works?
leftPictureSeen[i] = 1; // When an "a" div has been clicked, enable the "b" div click
});
$("#div_a_" + i.toString()).mouseout(function(){
$(this).animate({opacity:.15}, 1000);
});
$("#div_b_" + i.toString()).click(function(){
if (leftPictureSeen[i] === 1) // HOW DO I FIX THIS LINE SO THAT IT WORKS?
$(this).animate({opacity:1}, 1000);
else alert("You must click on the 'a' div first...");
});
$("#div_b_" + i.toString()).mouseout(function(){
$(this).animate({opacity:.15}, 1000);
});
};
可能有一种更简单的方法来完成这一切,也许是通过向“a”div 添加一个 css 属性来指示它们已被单击,我知道什么!
而且,如果这还不够,是否有人愿意向我解释,或者将我发送到某处的链接,解释“i”超出 .click 函数范围的问题,如果那是什么它叫什么?
非常感谢您的耐心……以及您的帮助。
最佳答案
解决该问题的一种方法如下,但请注意我没有使用每个元素的不必要的 id
属性,尽管我已经引入了两个 data-*
属性将允许 CSS 处理动画和淡入:
// binding the anonymous function of the on() method as
// the event-handler for the 'click' event received by
// any element with the 'pingpongdiv' class-name:
$('.pingpongdiv').on('click', function() {
// here we store the current 'letter' of the clicked
// element, retrieving it from the letter property of
// the HTMLElement.dataset object, which is the
// data-letter attribute from the element; then we
// convert that letter to its lower-case form (this
// ensures that it will always be lower-case in the
// following comparison):
let letter = this.dataset.letter.toLowerCase(),
// we cache the previousElementSibling:
previous = this.previousElementSibling;
// if the data-letter attribute is exactly equal to 'a':
if (letter === 'a') {
// we assign the String 'true' to the value of
// the HTMLElement.dataset.clicked property,
// which is also placed in the data-clicked
// attribute-value:
this.dataset.clicked = 'true';
// otherwise if the letter is exactly equal to 'b'
// AND there is a previous element AND that previous
// element has been clicked (its data-clicked is
// exactly equal to 'true'):
} else if (letter === 'b' && previous && previous.dataset.clicked === 'true') {
// we then assign the String of 'true' to
// the clicked 'b' element (having done this
// the CSS can now handle the animation):
this.dataset.clicked = 'true';
}
// you may notice there's no 'else' statement,
// this is by design since in the else we'd
// want nothing to happen anyway.
});
body {
text-align: center;
}
div {
box-sizing: border-box;
width: 40%;
height: 2em;
line-height: 2em;
display: inline-block;
border: 2px solid #000;
margin-bottom: 1em;
}
.pingpongdiv {
opacity: 0.2;
/* setting the transition/animation,
we transition all properties,
over a duration of 1 second, and
use a linear animation to do so: */
transition: all 1s linear;
}
.pingpongdiv[data-letter=a] {
border-radius: 1em 0 0 1em;
}
.pingpongdiv[data-letter=b] {
border-radius: 0 1em 1em 0;
}
.pingpongdiv[data-letter=a][data-clicked=true] {
border-color: #f90;
}
.pingpongdiv[data-letter=b][data-clicked=true] {
border-color: limegreen;
}
/* almost everything above this point is purely
aesthetic; this part handles the animations
which was defined above (and was the non-
aesthetic part above): */
/* here we select an element with a class of
'pingpongdiv' with a 'data-letter' attribute
equal to 'a' *and* has a 'data-clicked'
attribute equal to 'true': */
.pingpongdiv[data-letter=a][data-clicked=true],
/* here we select an element of class 'pingpongdiv'
with a 'data-letter' attribute equal to 'b' *and*
a 'data-clicked' attribute equal to 'true' which
follows an element matching the previous selector: */
.pingpongdiv[data-letter=a][data-clicked=true] + .pingpongdiv[data-letter=b][data-clicked=true] {
/* and updates the opacity property, which
will transition according to the above
transition property-value: */
opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div_a_1" class="pingpongdiv" data-letter="a">hkh</div>
<div id="div_b_1" class="pingpongdiv" data-letter="b">nuigyuig</div>
<div id="div_a_2" class="pingpongdiv" data-letter="a">uynyn</div>
<div id="div_b_2" class="pingpongdiv" data-letter="b">,jomoi</div>
<div id="div_a_3" class="pingpongdiv" data-letter="a">yfvrcers</div>
<div id="div_b_3" class="pingpongdiv" data-letter="b">rdcsers</div>
您可能已经从上面代码中的大部分纯 JavaScript 方法中注意到,jQuery 在任何方面都不是满足此问题要求所必需的;所以下面是一个纯 JavaScript 解决方案:
// here we create a named function to handle the checks
// and updates to the data-clicked attributes:
function pairAnimate() {
// 'this' is passed automatically to the function
// from the EventTarget.addEventListener() method
// used later.
// there are absolutely no changes to any parts
// of what was the anonymous function (except
// for its being named, to no longer be an
// anonymous function):
let letter = this.dataset.letter.toLowerCase(),
previous = this.previousElementSibling;
if (letter === 'a') {
this.dataset.clicked = true;
} else if (letter === 'b' && previous && previous.dataset.clicked === 'true') {
this.dataset.clicked = true;
}
}
// retrieving a NodeList of all <div> elements that have
// a 'data-letter' attribute, and using Array.from() to
// convert that Array-like NodeList into an Array:
var letterElements = Array.from(document.querySelectorAll('div[data-letter]'));
// iterating over each element in the Array of letterElements
// using Array.prototype.forEach():
letterElements.forEach(
// here we use an Arrow function to bind the
// the (poorly-named) 'pairAnimate' function as
// handler for the 'click' event:
letterElement => letterElement.addEventListener('click', pairAnimate)
);
body {
text-align: center;
}
div {
box-sizing: border-box;
width: 40%;
height: 2em;
line-height: 2em;
display: inline-block;
border: 2px solid #000;
margin-bottom: 1em;
}
.pingpongdiv {
opacity: 0.2;
transition: all 1s linear;
}
.pingpongdiv[data-letter=a] {
border-radius: 1em 0 0 1em;
}
.pingpongdiv[data-letter=b] {
border-radius: 0 1em 1em 0;
}
.pingpongdiv[data-letter=a][data-clicked=true] {
border-color: #f90;
}
.pingpongdiv[data-letter=b][data-clicked=true] {
border-color: limegreen;
}
.pingpongdiv[data-letter=a][data-clicked=true],
.pingpongdiv[data-letter=a][data-clicked=true] + .pingpongdiv[data-letter=b][data-clicked=true] {
opacity: 1;
}
<div id="div_a_1" class="pingpongdiv" data-letter="a">hkh</div>
<div id="div_b_1" class="pingpongdiv" data-letter="b">nuigyuig</div>
<div id="div_a_2" class="pingpongdiv" data-letter="a">uynyn</div>
<div id="div_b_2" class="pingpongdiv" data-letter="b">,jomoi</div>
<div id="div_a_3" class="pingpongdiv" data-letter="a">yfvrcers</div>
<div id="div_b_3" class="pingpongdiv" data-letter="b">rdcsers</div>
引用资料:
data-*
attributes .关于Jquery 在 div 上创建双数组 .click 函数,用于 div 之间的交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259697/
将 .off 与 .click 结合使用有何用途或区别? $('#link').off('click').on('click',function(){}); 对比 $('#link').on('cli
我正在学习 javascript,并且我见过不止一种注册点击事件的方法: $(DOMelement).click(function() {}); $(DOMelement).on('click',fu
我习惯使用.click()和delegate('click') ,所以当我读到这两个在 jQuery 的最新版本中都被弃用时,我想我应该读一下它,但我有点摸不着头脑。 文档 here似乎表明这是 .l
我已经使用它们很长一段时间了,但大多数时候,我更喜欢较短的,但是,我只是想真正深入了解本质细节。我可能一直在创建有错误的代码,并且我不想在网络上贡献和传播懒惰完成的代码。 所以,告诉我: What a
我想实现类似在 Android 上点击 Instagram 中的帖子的功能(我认为在 iOS 上应该是相同的功能)。在 Instagram 中,如果有人点击照片,它会打开,双击,它会被喜欢,然后单击并
我的点击事件有问题。它运行 1 次。示例: 我有 3 页(1 2 3)。当我点击 2 时它起作用了。然后在第 3 页,它又可以工作了。但我再次点击 2 不起作用。该事件未触发。 $("div .top
我正在尝试这样做。在 jsfiddle 中:http://jsfiddle.net/QGGhW/2/ 但是在我点击 not now 之后,下一个文本输入就不再可点击了。 我该怎么做?是否有类似 .on
我想知道是否有任何情况下使用 .click(function {...}); 而不是 .live('click', function { ...});? 据我所知,实时选项似乎是一个更好的选择,因此我
我正在尝试在 Accordion 内部创建一个 Accordion ......并且我有点挣扎。 本质上,我有一个 div .applicant,单击它会添加一个 .expand 类,它将高度设置为
我可以使用一些帮助来调试这个: var factBody = jQuery(".fact__body"); jQuery(".fact").on("click", function() { j
如果我有一个用于 clicked 类的监听器,以及另一个用于 click-option-1、click-option-2 类的监听器,以及click-option-3,如何在我的 clicked 事件
下面的代码有什么区别吗? $('#whatever').on('click', function() { /* your code here */ }); 和 $('#whatever').
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我不擅长 UI 设计,所以我使用 bootstrap 和 jquery 来帮助我一点。我正在尝试将 jQuery 事件绑定(bind)到导航栏,因此当用户单击导航栏上的 p 标签时,它应该触发单击事件
问题是,当我单击 delaccbut 按钮时,单击功能起作用并显示消息,但是当我单击 confdel 或 redel 按钮来自 click 函数,它不...为什么? HTML: Delete my
我想要完成的是“类似”的东西 $("button .toggleExcerpt) .toggle( function FIRST() { do first function..
function show1(){ $("#btn").click(show2()); window.alert(
我不明白为什么这不起作用。当我单击具有该类名的复选框时,clickId 被赋予值access-toggle-all,所以这没有意义(至少对我来说).. $(document).ready(functi
我有一个关于 click() 的复杂问题。我有 4 个按钮,分别命名为功能 1、功能 2、方法 1 和方法 2。 Function 1 Function 2 Method 1 Method 2 当单
最后,我认为这不是我特别需要解决的问题,但我不明白为什么会这样,这让我很困扰。 基本上,我有一些复选框,我只希望用户能够选择其中的一定数量。我正在使用下面的代码来实现该效果。 $j( function
我是一名优秀的程序员,十分优秀!