简体   繁体   English

根据其数据属性选择单击的元素

[英]Selecting a clicked element based on its data attribute

I'm working on a personal trainer website that lets you choose the time of your training session. 我正在一个私人教练的网站上,您可以选择培训时间。 There are 6 times available: 5 mins, 10 mins, 15 mins, and so on. 有6次可用:5分钟,10分钟,15分钟,依此类推。 This is what I have so far. 到目前为止, 就是我所拥有的。

I want to highlight the time that you've chosen. 我想强调一下您选择的时间。 I've only tried to implement this for the top row of times, and when you click a time to highlight it the other ones don't get unhighlighted, but that is not why I'm asking this question. 我只是尝试在最前面的时间实现此功能,当您单击某个时间以突出显示它时,其他功能并不会被高亮显示,但这不是我问这个问题的原因。 My problem is that when you click on a time, nothing happens. 我的问题是,当您单击某个时间时,没有任何反应。 I've tried looking in the console and this is the error it throws: 我尝试在控制台中查找,这是它引发的错误:

在此处输入图片说明

What's wrong with my code? 我的代码有什么问题?

 $("body").append( "<p class='text' id='CYTText'>Choose your session's time:</p>" ); setUpCYT(350, 200, 0.8, 0.85); function setUpCYT(littleXOffset, littleYOffset, littleScale, littleOpacity) { for (i = 1; i < 4; i++) { var timeSelectorElement = "[data='" + i + "']"; var timeSelectorName = "timeSelector"+i; $("body").append( "<p class='text' id='CYTTimerText' data='" + i + "' onclick='selectTime(" + timeSelectorElement + ")'>00:00</p>" ); $("[data='" + i + "']").css({"left":littleXOffset * (i-2), "-webkit-transform":"scale(" + littleScale + ")", "opacity":littleOpacity}); timeSelectorName = new Timer(timeSelectorElement); timeSelectorName.set(i*5,0); } for (i = 1; i < 4; i++) { $("body").append( "<p class='text' id='CYTTimerText' data='" + i+3 + "'>00:00</p>" ); $("[data='" + i+3 + "']").css({"top":littleYOffset, "left":littleXOffset * (i-2), "-webkit-transform":"scale(" + littleScale + ")", "opacity":littleOpacity}); var timeSelectorElement = "[data='" + i+3 + "']"; var timeSelectorName = "timeSelector"+i+3; timeSelectorName = new Timer(timeSelectorElement); timeSelectorName.set((i+3)*5,0); } //select the middle selectTime("[data='2']"); } function selectTime(data) { TweenLite.to($(data), 0.5, { "-webkit-transform":"scale(1)", "opacity":1 }); } //timer function function Timer (element) { var minutes, seconds, finalTimeInSeconds, displayMinutes, displaySeconds, interval = 1000, self = this, timeLeftToNextSecond = 1000, running = false; this.set = function(inputMinutes, inputSeconds) { finalTimeInSeconds = inputMinutes * 60 + inputSeconds; minutes = (Math.floor(finalTimeInSeconds / 60)); seconds = finalTimeInSeconds % 60; this.print(); } this.add = function(inputMinutes, inputSeconds) { finalTimeInSeconds += inputMinutes * 60 + inputSeconds; finalTimeInSeconds = (finalTimeInSeconds < 0) ? 0 : finalTimeInSeconds; minutes = (Math.floor(finalTimeInSeconds / 60)); seconds = finalTimeInSeconds % 60; this.print(); } this.subtract = function(inputMinutes, inputSeconds) { finalTimeInSeconds -= inputMinutes * 60 + inputSeconds; if(finalTimeInSeconds < 0) {nextTask()} finalTimeInSeconds = (finalTimeInSeconds < 0) ? 0 : finalTimeInSeconds; minutes = (Math.floor(finalTimeInSeconds / 60)); seconds = finalTimeInSeconds % 60; this.print(); } this.reset = function() { this.set(0,0); } this.print = function() { displayMinutes = (minutes.toString().length == 1) ? "0" + minutes : minutes; //ternary operator: adds a zero to the beggining displaySeconds = (seconds.toString().length == 1) ? "0" + seconds : seconds; //of the number if it has only one caracter. $(element).text(displayMinutes + ":" + displaySeconds); } this.run = function() { if (running == false) { running = true; var _f = function() { secondStarted = new Date; self.subtract(0, 1); interval = 1000; var theColorIs = $(element).css("color"); ac = setTimeout(_f,interval); } ac = setTimeout(_f, interval); } } this.stop = function() { if (running == true) { running = false; stopped = new Date; interval = 1000 - (stopped - secondStarted); clearTimeout(ac); } } } 
 body{ background-color: #02BFC1; overflow:hidden; margin: 0; } @font-face { font-family: 'Bebas Neue'; font-style: normal; font-weight: normal; src: local('Bebas Neue'), url('BebasNeue.woff') format('woff'); } .text { color: #F1F2F0; font-family:Bebas Neue; -webkit-user-select: none; cursor: default; text-shadow: 3px 3px 2px rgba(0,0,0,0.2); } #CYTText { text-align:center; height: 100px; position: absolute; margin: auto; top: 0; bottom: 290px; right: 0; left: 0; font-size:50px; } #CYTTimerText { text-align:center; height: 100px; position: absolute; margin: auto; top: 0; bottom: 150px; right: 0; left: 0; font-size:95px; } 

There are a couple of simple issues. 有几个简单的问题。 first off the string isn't being parsed correctly that is setting up the onclick 首先,该字符串未正确解析,这是在设置onclick

  1. Closing quotes 结束语

It should look like this: onclick="selectTime("[data='2']")" But it looks like this: onclick="selectTime([data=" 2'])' 它看起来应该像这样: onclick="selectTime("[data='2']")"但是它看起来像这样: onclick="selectTime([data=" 2'])'

  1. Reference Error 参考误差

There is also a scoping /reference error for the function selectTime the click is trying to call 点击尝试调用的函数selectTime的作用域/参考错误

Here is the full working example: http://jsfiddle.net/qLnLmgy6/2/ 这是完整的工作示例: http : //jsfiddle.net/qLnLmgy6/2/

Hope that helps! 希望有帮助!

The syntax error is caused by the string that you're passing to append() : 语法错误是由传递给append()的字符串引起的:

$("body").append(
        "<p class='text' id='CYTTimerText' data='" + i +
        "' onclick='selectTime(" + timeSelectorElement + ")'>00:00</p>"
);

Let's print an instance of this string and examine it: 让我们打印此字符串的一个实例并进行检查:

<p class='text' id='CYTTimerText' data='1' onclick='selectTime([data='1'])'>00:00</p>

Observe that the onclick handler is a code fragment delimited by single quotes: 请注意, onclick处理程序是由单引号分隔的代码片段:

onclick='selectTime([data='

We can fix this by replacing the single quotes with escaped double quotes: 我们可以通过将单引号替换为转义的双引号来解决此问题:

$("body").append(
        "<p class='text' id='CYTTimerText' data='" + i +
        "' onclick=\"selectTime(" + timeSelectorElement + ")\">00:00</p>"
);

However, now we have another problem. 但是,现在我们还有另一个问题。 An instance of the string looks like this: 字符串的一个实例如下所示:

<p class='text' id='CYTTimerText' data='1' onclick="selectTime([data='1'])">00:00</p>

The code for onclick is selectTime([data='1']) , which is syntactically incorrect. onclick的代码是selectTime([data='1']) ,在语法上不正确。 The intention is to pass the string "[data='1']" to selectTime . 目的是将字符串"[data='1']"传递给selectTime

The inline HTML already uses double quotes to delimit the onclick value. 内联HTML已经使用双引号分隔了onclick值。 How do we put double quotes inside this value? 我们如何在该值内加上双引号? We have to use &quot; 我们必须使用&quot; for each double quote: 对于每个双引号:

$("body").append(
        "<p class='text' id='CYTTimerText' data='" + i + "' onclick=\"selectTime(&quot;" + timeSelectorElement + "&quot;)\">00:00</p>"
);

Now an instance of the string looks like this: 现在,该字符串的一个实例如下所示:

<p class='text' id='CYTTimerText' data='1' onclick="selectTime(&quot;[data='1']&quot;)">00:00</p>

That looks strange, but it will be correct once it has been inserted into the document. 这看起来很奇怪,但是一旦将其插入文档中便是正确的。

After making that change, the code sort of works. 进行更改后,代码就可以工作了。 It's still not right because you have layout problems, but at least you can click on the 15:00 time and see that the onclick handler calls selectTime correctly. 仍然不正确,因为存在布局问题,但是至少您可以单击15:00时间,并看到onclick处理程序正确调用了selectTime

By the way, there are better approaches than building that complicated string. 顺便说一下,有比构建复杂字符串更好的方法。 You can simplify the inline handler to onclick="selectTime(this)" , where this will have the value of the object that was clicked. 您可以简化嵌入式处理程序onclick="selectTime(this)" ,在this将有被点击的对象的值。 An even better way to go about it would be to avoid inline handler definitions. 一种更好的解决方法是避免内联处理程序定义。 Instead, build a paragraph object and make a new function that you assign as a click handler. 而是,构建一个段落对象并创建一个新函数,将其分配为单击处理程序。

Regardless of how you implement the onclick handler, you're left with the problem of overlapping timer elements. 无论您如何实现onclick处理程序,都将面临计时器元素重叠的问题。 Your timers are paragraphs that you've absolutely positioned next to one another. 计时器是您绝对相邻的段落。 The paragraphs stretch as wide as possible. 段落尽可能宽。 Thus, the first timer is obscured by subsequent timers. 因此,第一定时器被随后的定时器遮盖。

You can get rid of the overlap by displaying the timers as inline-block elements. 您可以通过将计时器显示为inline-block元素来消除重叠。 To restrict the width of the layout, put everything into a wrapper div . 要限制布局的宽度,请将所有内容放入包装器div The following snippet demonstrates this approach. 下面的代码段演示了这种方法。

 window.onload = function () { $('#wrapTimers').append( "<p class='text' id='CYTText'>Choose your session duration:</p>" ); setUpCYT(350, 200, 0.8, 0.85); }; function setUpCYT(littleXOffset, littleYOffset, littleScale, littleOpacity) { for (i = 1; i < 4; i++) { var timeSelectorElement = "[data='" + i + "']"; var timeSelectorName = "timeSelector"+i; var s = "<p class='text timerContainer' data='" + i + "' onclick=\\"selectTime(this)\\">00:00</p>"; //"' onclick=\\"selectTime(&quot;" + timeSelectorElement + "&quot;)\\">00:00</p>"; $('#wrapTimers').append(s); $("[data='" + i + "']").css({"left":littleXOffset * (i-2), "-webkit-transform":"scale(" + littleScale + ")", "opacity":littleOpacity}); timeSelectorName = new Timer(timeSelectorElement); timeSelectorName.set(i*5,0); } for (i = 1; i < 4; i++) { $('#wrapTimers').append( "<p class='text timerContainer' data='" + i+3 + "'>00:00</p>" ); $("[data='" + i+3 + "']").css({"top":littleYOffset, "left":littleXOffset * (i-2), "-webkit-transform":"scale(" + littleScale + ")", "opacity":littleOpacity}); var timeSelectorElement = "[data='" + i+3 + "']"; var timeSelectorName = "timeSelector"+i+3; timeSelectorName = new Timer(timeSelectorElement); timeSelectorName.set((i+3)*5,0); } //select the middle selectTime("[data='2']"); } function selectTime(selector) { TweenLite.to($(selector), 0.5, { "-webkit-transform":"scale(1)", "opacity":1 }); } //timer function function Timer (element) { var minutes, seconds, finalTimeInSeconds, displayMinutes, displaySeconds, interval = 1000, self = this, timeLeftToNextSecond = 1000, running = false; this.set = function(inputMinutes, inputSeconds) { finalTimeInSeconds = inputMinutes * 60 + inputSeconds; minutes = (Math.floor(finalTimeInSeconds / 60)); seconds = finalTimeInSeconds % 60; this.print(); } this.add = function(inputMinutes, inputSeconds) { finalTimeInSeconds += inputMinutes * 60 + inputSeconds; finalTimeInSeconds = (finalTimeInSeconds < 0) ? 0 : finalTimeInSeconds; minutes = (Math.floor(finalTimeInSeconds / 60)); seconds = finalTimeInSeconds % 60; this.print(); } this.subtract = function(inputMinutes, inputSeconds) { finalTimeInSeconds -= inputMinutes * 60 + inputSeconds; if(finalTimeInSeconds < 0) {nextTask()} finalTimeInSeconds = (finalTimeInSeconds < 0) ? 0 : finalTimeInSeconds; minutes = (Math.floor(finalTimeInSeconds / 60)); seconds = finalTimeInSeconds % 60; this.print(); } this.reset = function() { this.set(0,0); } this.print = function() { displayMinutes = (minutes.toString().length == 1) ? "0" + minutes : minutes; //ternary operator: adds a zero to the beggining displaySeconds = (seconds.toString().length == 1) ? "0" + seconds : seconds; //of the number if it has only one caracter. $(element).text(displayMinutes + ":" + displaySeconds); } this.run = function() { if (running == false) { running = true; var _f = function() { secondStarted = new Date; self.subtract(0, 1); interval = 1000; var theColorIs = $(element).css("color"); ac = setTimeout(_f,interval); } ac = setTimeout(_f, interval); } } this.stop = function() { if (running == true) { running = false; stopped = new Date; interval = 1000 - (stopped - secondStarted); clearTimeout(ac); } } } 
 body { background-color: #02BFC1; overflow: hidden; margin: 0; } #wrapTimers { width: 800px; margin: 40px auto; text-align: center; } .text { color: #F1F2F0; font-family: Oswald, sans-serif; -webkit-user-select: none; cursor: default; text-shadow: 3px 3px 2px rgba(0, 0, 0, 0.2); } #CYTText { width: 800px; margin: auto; font-size: 50px; } .timerContainer { text-align: center; display: inline-block; font-size: 95px; margin: 0 10px; } 
 <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Oswald" > <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenLite.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/plugins/CSSPlugin.min.js"></script> <div id="wrapTimers"></div> 

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

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