- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个测验,其中有两个选项可以回答问题,滑动(向右或向左)或单击(心形或划掉心形)选项。
我可以使用滑动功能并正确地制表分数,但我无法让点击事件运行用于计算答案是否正确的参数。
我的 HTML 看起来像这样:
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Quiz</title>
<link rel="stylesheet" href="css/style.css">
<link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
<script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
<script src="http://code.ionicframework.com/collide/0.0.4/collide.js"></script>
<script src="http://www.loringdodge.com/files/ionic.tdcards2.js"></script>
</head>
<body ng-app="starter" no-scroll>
<ion-pane ng-controller="CardsCtrl" class="background-grey">
<ion-header-bar class="bar-default">
<h1 class="title">Quiz</h1>
</ion-header-bar>
<div class="td-title">
<div class="row">
<div class="col">Master: <span>{{ cards.master.length }}</span></div>
<p style="color:blue;"><strong>Points</strong> : <span>{{points}}</span></p>
<div class="col">Bands: <span>{{ cards.active.length }}</span></div>
<div class="col">Discards: <span>{{ cards.discards.length }}</span></div>
</div>
<div class="row">
<div class="col">Liked: <span>{{ cards.liked.length }}</span></div>
<div class="col">Disliked: <span>{{ cards.disliked.length }}</span></div>
</div>
</div>
<h2 id="family-zone">SWIPE RIGHT<br>FOR FAMILY</h2>
<h2 id="not-family-zone">SWIPE LEFT<br>FOR NOT FAMILY</h2>
<!-- *************************
TD Cards
- We start off with an ng-if so that the cards are not generated
unless $scope.cards is not 'null'
************************* -->
<div ng-if="cards.active">
<td-cards>
<td-card ng-repeat="card in cards.active" on-destroy="cardDestroyed($index)" on-swipe-left="cardSwipedLeft($index)" on-swipe-right="cardSwipedRight($index)">
<div class="image" ng-controller="CardCtrl">
<div class="no-text" ng-click="onClickTransitionOut(card)($index)" ng-click="onClickNotFamily(card)($index)"></div>
<div class="yes-text" ng-click="onClickTransitionOut(card)($index)" ng-click="onClickFamily(card)($index)"></div>
<!-- *************************
Discard
- The card is removed from the deck and a fly away animation is triggered.
- onClickTransitionOut is found in ionic.tdcards.js
- Animation can be customized by changing defaults
************************* -->
<img ng-src="{{ card.image }}">
</div>
</td-card>
<!-- *************************
End Card
- 'drag' is set to false. The user cannot drag it.
- 'refreshCards()' reloads all cards that have NOT been discarded.
************************* -->
<td-card id="end-card" drag="false">
<h2 id="share">SHARE ON FACEBOOK</h2>
<!-- <i class="icon ion-ios-refresh disable-user-behavior" ng-click="refreshCards()"></i> -->
</td-card>
</td-cards>
</div>
</ion-pane>
</body>
我的 Javascript 目前看起来像这样:
<script type="text/javascript">
angular.module('starter', ['ionic', 'ionic.contrib.ui.tinderCards2'])
.config(function($stateProvider, $urlRouterProvider) {
})
.directive('noScroll', function($document) {
return {
restrict: 'A',
link: function($scope, $element, $attr) {
$document.on('touchmove', function(e) {
e.preventDefault();
});
}
}
})
.controller('CardsCtrl', function($scope, TDCardDelegate, $timeout) {
var cardTypes = [
{
"image": 'http://classicsdujour.com/wp2015/wp-content/uploads/2015/01/Van-Halen-Deluxe-Cover.jpg',
"question" : "Q: This question is true",
"valid" : 1, // indicates the correct array number, use 0, 1...
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"image": 'https://www.dustygroove.com/images/products/j/jackson5~~~_abc180gra_101b.jpg',
"question" : "Q: This answer to this question is true",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"image": 'https://fanart.tv/fanart/music/9beb62b2-88db-4cea-801e-162cd344ee53/albumcover/solid-gold-hits-504adea20593c.jpg',
"question" : "Q: This question is false",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"image": 'http://cdn.therockcorner.com/artists/k/kings-of-leon/albums/youth-and-young-manhood-5.jpg',
"question" : "Q: The answer to this question is truuueee",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"image": 'https://analogkidblog.files.wordpress.com/2015/06/the-family-320-kbps.png',
"question" : "Q: False is the answer here",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is false"]
},
{
"image": 'https://upload.wikimedia.org/wikipedia/en/1/1c/SoundsSilence.jpg',
"question" : "Q: This questions is false",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"image": 'http://static1.squarespace.com/static/5073209bc4aa6253939f5db3/t/5715299c40261d3e0a616695/1461004706936/?format=500w',
"question" : "Q: True True Truueee",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"image": 'https://upload.wikimedia.org/wikipedia/en/a/ae/Daft_Punk_-_Discovery.jpg',
"question" : "Q: This one is indeed false",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
{
"image": 'http://66.media.tumblr.com/tumblr_lidp3btJqJ1qzk6vno1_1280.jpg',
"question" : "Q: Ahaaaa a true question",
"valid" : 1,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is True"]
},
{
"image": 'https://upload.wikimedia.org/wikipedia/en/2/20/Arcticmonkeys-humbug.jpg',
"question" : "Q: Another false statement",
"valid" : 0,
"buttons" : ["False", "True"],
"answers" : [ "The correct answer is False"]
},
//{
//"question" : "Game over, check your score below.",
//}
];
var qc = 0, // Current Question counter
points = 0, // Current points
validIdx = cardTypes.valid,
$points = ("p>span");
$scope.cards = {
master: Array.prototype.slice.call(cardTypes, 0),
active: Array.prototype.slice.call(cardTypes, 0),
discards: [],
liked: [],
disliked: []
}
$scope.points = 0;
$scope.cardDestroyed = function(index) {
$scope.cards.active.splice(index, 1);
};
$scope.addCard = function() {
var newCard = cardTypes[0];
$scope.cards.active.push(angular.extend({}, newCard));
}
$scope.refreshCards = function() {
// Set $scope.cards to null so that directive reloads
$scope.cards.active = null;
$timeout(function() {
$scope.cards.active = Array.prototype.slice.call($scope.cards.master, 0);
});
$scope.points = 0;
}
$scope.$on('removeCard', function(event, element, card) {
var discarded = $scope.cards.master.splice($scope.cards.master.indexOf(card), 1);
$scope.cards.discards.push(discarded);
});
$scope.cardSwipedLeft = function(index) {
console.log('LEFT SWIPE');
var card = $scope.cards.active[index];
$scope.cards.disliked.push(card);
var givenAnswer = 0,
correctAnswer = card.valid;
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
} else {
console.log('incorrect!!!!');
}
//increment the points variable if the answer is correct:
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
$scope.points++;
}
};
$scope.cardSwipedRight = function(index) {
console.log('RIGHT SWIPE');
var card = $scope.cards.active[index];
$scope.cards.liked.push(card);
var givenAnswer = 1,
correctAnswer = card.valid;
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
} else {
console.log('incorrect!!!!');
}
//increment the points variable if the answer is correct:
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
$scope.points++;
}
};
$scope.onClickNotFamily = function(index) {
console.log('LEFT CLICK');
var givenAnswer = 0,
correctAnswer = card.valid;
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
} else {
console.log('incorrect!!!!');
}
//increment the points variable if the answer is correct:
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
$scope.points++;
}
};
$scope.onClickFamily = function(index) {
console.log('RIGHT CLICK');
var card = $scope.cards.active[index];
$scope.cards.liked.push(card);
var givenAnswer = 1,
correctAnswer = card.valid;
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
} else {
console.log('incorrect!!!!');
}
//increment the points variable if the answer is correct:
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
$scope.points++;
}
};
})
.controller('CardCtrl', function($scope, TDCardDelegate) {
});
</script>
我只想让点击事件运行参数以确定答案是否正确。
例如,当用户向右滑动时运行此参数的当前脚本片段如下所示:
$scope.cardSwipedRight = function(index) {
console.log('RIGHT SWIPE');
var card = $scope.cards.active[index];
$scope.cards.liked.push(card);
var givenAnswer = 1,
correctAnswer = card.valid;
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
} else {
console.log('incorrect!!!!');
}
//increment the points variable if the answer is correct:
if (givenAnswer === correctAnswer) {
console.log('correct!!!!');
$scope.points++;
}
};
最佳答案
来自HTML spec :
When the user agent leaves the attribute name state (and before emitting the tag token, if appropriate), the complete attribute's name must be compared to the other attributes on the same token; if there is already an attribute on the token with the exact same name, then this is a parse error and the new attribute must be removed from the token.
意思是,当你写的时候:
<div class="no-text" ng-click="onClickTransitionOut(card)($index)" ng-click="onClickNotFamily(card)($index)"></div>
第一个ng-click
属性 ( onClickTransitionOut...
) 被分配,第二个 ( onClickNotFamily
) 被丢弃,因为 ng-click
已经有值了.因此,只有第一个函数会在您单击时运行。
这里有两个选项可以解决这个问题,以便运行两个函数:
重构您的代码,以便有一个函数可以绑定(bind)到 ng-click
.我建议这样做,因为您的 JS 似乎在点击和滑动处理程序之间有一些复制/粘贴代码。
合并ng-click
属性为ng-click="function1(); function2()"
(如 this post 中所示)。
希望对您有所帮助!我不是 Angular 专家,所以可能还有其他我没有注意到的问题。
关于javascript - 如何使用js增加onclick事件的积分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39334370/
这里有这样的问题,但我尝试了解决方案,但在我的情况下这不起作用。我有一个通过单击打开的表单,然后必须通过单击此表单内的 x 将其关闭。 This variant不适合,因为 cookie 在关闭表单后
我有以下代码: function sdefaults() { alert("test"); } var btnpos, sbtn; btnpos = document.getElementsBy
我正在学习 javascript 和 php。我创建了一个联系表单,我希望按下提交按钮时可以完成两件事: 将数据提交给我(这部分正在运行) 阅读我的 onclick 函数(这部分不起作用) 我正在
我的页面有多个div元素,有显示切换功能。 onclick = "document.getElementById('light').style.display='block';document.ge
为什么不同? document.getElementById('click').onclick = a.replaceChild(c,b) --- 在我不点击按钮的情况下替换元素 但 onclick=
我以为在javascript中绑定(bind)点击事件是通过node.onclick来完成的,Chrome/Firefox似乎也同意我的看法,但我看到它写成.onClick here 3个人4次,所以
基本上,我想创建一个侧边栏,其中会有很多联系人。每个联系人都会出现在一个列表中。列表中有两个 onclick 函数。列表中两个 onclick 函数是:第一次点击-点击列表后它应该打开另一个侧边栏(我
我是 Javascript 的新手,尤其是在 JSON 和 Jquery 上。我已经成功实现了 FullCalendar 的月 View ,但是无论如何要根据用户单击月 View 的日期来显示或弹出日
给定一些简单的 HTML,例如,其中一个元素具有 onclick 函数,并且它的子元素也具有 onclick 函数: 正确的方法是什么,以便当用户单击子元素时,仅执行子元素的 oncli
我有一个功能,当您单击一个按钮时,TinyMCE 框内的选定文本被包装在 span 中标签。 这是这样做的: var apolo = '' + sel + ''; tinyMCE.activeEdit
我尝试使用 JavaScript 将 onclick 事件替换为其他 onclick 事件: 旧 onClick 事件: $('#myButton').click(function(){ a
我正在尝试删除一个函数中的事件监听器,该函数与 onclick 调用的函数相同。 下面的例子说明了我的问题。我尝试使用这种构造的原因是将函数传递给 View 对象,以便它可以将其作为 onclick
这是一个相当简单的可访问性问题。 假设我们有一个切换链接/按钮,单击该链接/按钮即可切换移动菜单。对于这样的按钮,哪种格式更易于访问? 我们总共有 4 个选项,用 分隔 button标签与 a标签,
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to make onclick automatically through onload function
我需要能够改变一个 id 的 onclick 事件,这样一旦它被点击,一旦它执行一个改变 onclick 事件的函数 这是我的代码:Javascript: function showSearchBar
有谁知道如何将 onClick 分配给执行一个操作的对象,然后当用户点击同一对象时,该操作被反转? $('#image').click(function() { $('#foo').css({
我重写了 onClick(View) 方法,以便在单击时 View 大小会发生变化。View 是一个显示浏览器网页的 webView。我需要点击会改变大小,而且浏览器会响应(转到被点击的链接)。在目前
我有这个 HTML 代码: APPLY JS: $('#changeType').click(function(){ $('#discountCode').attr('type','pass
我想用 onclick 创建一个函数,如果我按下 div 就会显示内容。如果我点击该内容,它会给我“开始”表单。 让我向您展示代码: HTML: click Javascript: var div =
我编写了一个函数,用于使用超链接的占位符打开/关闭 div。 Open 然后 onclick 事件打开 div。 我有这个onclick来关闭它: Close 我想要做的是为在两个 onclick 事
我是一名优秀的程序员,十分优秀!