javascript - 使用angularjs用另一个图像替换一个图像

标签 javascript css angularjs html

我想在考试结束时使用 AngularJs 和 html 替换在线试卷上的图片。

在开始考试时,我已经在我的代码中使用了 img id="online_start",我使用 css 将图像放入我的页面。

CSS:

  #online_start{
    background-image:url("start_exam.jpg");
    background-repeat:no-repeat;
    width:400px;
    height:150px;
    }

现在我想使用 AngularJs 将考试结束时的那张图片替换为另一张图片。

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UFT-8">
<link rel="stylesheet" href="menu.css">
<link rel="stylesheet" href="layout.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
var app = angular.module('MyApp', [])
app.controller('MyController', function($scope, $compile, $window, $interval, $timeout, $filter) {

  $scope.pos = 0, $scope.correct = 0, $scope.ques = true;

  $scope.questions = [
    ["Which of the following a is not a keyword in Java ?", "class", "interface", "extends", "C"],

    ["Which of the following is an interface ?", "Thread", "Date", "Calender", "A"],

    ["Which company released Java Version 8 ?", "Sun", "Oracle", "Adobe", "A"],

    ["What is the length of Java datatype int ?", "32 bit", "16 bit", "None", "C"],

    ["What is the default value of Java datatype boolean?", "true", "false", "0", "A"]
  ];
  $scope.totalsecoriginal = $scope.totalsec = 60;
  $scope.totalsec--;
  $scope.min = parseInt($scope.totalsec / 60, 10);
  $scope.sec = $scope.totalsec - ($scope.min * 60);
  $scope.date = new Date();
  $scope.hhmmss = $filter('date')(new Date(), 'hh:mm:ss a');
  $scope.currentTime = new Date();
  $scope.currentTime.setSeconds($scope.currentTime.getSeconds() + 60);
  function _(x) {
    console.log(angular.element(document.getElementById(x)));
    return angular.element(document.getElementById(x))[0];
  }
  $scope.interval = $interval(function() {
    if ($scope.sec === 0) {
      $scope.min--;
      $scope.sec = 60;
    }
    $scope.sec--;
  }, 1000);
  $scope.$watch('sec', function() {
    if ($scope.min === 0 && $scope.sec === 0) {
      $interval.cancel($scope.interval);
      window.alert('Time Up!!!');
      $scope.pos = $scope.questions.length;
      $scope.temp = true;
      $scope.renderQuestion();
    }
  })
  $scope.renderQuestion = function() {
    if ($scope.pos >= $scope.questions.length) {
    $scope.myBackgroundUrl = 'url(animatedthankyou.gif)';

      $scope.ques = false;
      if (!$scope.temp) { $scope.temp = false;
        $interval.cancel($scope.interval);
      }
      $scope.showscore = Math.round($scope.correct / $scope.questions.length * 100);
      $scope.minuteleft = parseInt(($scope.totalsecoriginal - $scope.totalsec) / 60, 10);

      $scope.pos = 0;
      return false;
    }
    $scope.question = $scope.questions[$scope.pos][0];
    $scope.chA = $scope.questions[$scope.pos][1];
    $scope.chB = $scope.questions[$scope.pos][2];
    $scope.chC = $scope.questions[$scope.pos][3];
  }
  $scope.checkAnswer = function() {
    $scope.choices = angular.element(document.getElementsByName('choices'));
    $scope.choice = -1;
    for (var i = 0; i < $scope.choices.length; i++) {
      if ($scope.choices[i].checked) {
        $scope.choice = $scope.choices[i].value;
        $scope.choices[i].checked = false;
      }
    }
    if ($scope.choice == $scope.questions[$scope.pos][4]) {
      $scope.correct++;
    }
    $scope.pos++;
    $scope.renderQuestion();
  }
  $scope.renderQuestion();

});

</script>
</head>
<body>
  <div id="Holder">
    <div id="Header"></div>
    <div id="NavBar">
<nav>
      <ul>
        <li><a href="Loginpage.htm"> Login</a></li>
        <li><a href="Registrationpage.htm">Registration</a></li>
      </ul>
    </div>
    <div id="Content">
      <div id="PageHeading">
        <h1><marquee direction="right" behavior="alternate">All the Best</marquee></h1>
      </div>
      <div id="ContentLeft">
        <h2></h2>
        <br>
        <img id="online_start" ng-style="{background-image: myBackgroundUrl}">
        <br>
        <h6>Online Examination System(OES) is a Multiple Choice Questions(MCQ) based 
examination system that provides an easy to use environment for both 
Test Conducters and Students appearing for Examination.</h6>
      </div>
      <div id="ContentRight">
        <section class="loginform_cf">
          <div ng-app="MyApp" ng-controller="MyController" ng-init="StartTimer()">
            <table>
              <tr>
                <td id="test_status" style="text-align:left">
                  <h3 ng-show='ques'>Question {{pos+1}} of {{questions.length}}</h3>
                  <h3 ng-hide='ques'>Test Completed  </h3>
                </td>
                <td ng-show='ques'> Exam Starts :<span ng-bind="hhmmss">  </span> </td>
                <td ng-show='ques'> Exam Ends : {{currentTime | date:'hh:mm:ss a'}} </td>
              </tr>
              <tr>
                <td id="test" colspan="3">
                  <div ng-show="ques">
                    <h3>{{question}}</h3>
                    <input type='radio' name='choices' value='A'>{{chA}}
                    <br>
                    <input type='radio' name='choices' value='B'>{{chB}}
                    <br>
                    <input type='radio' name='choices' value='C'>{{chC}}
                    <br>
                    <br>
                    <button ng-click='checkAnswer()'>Next</button>
            <br>
            <br>
            Your Left Time is :{{min}} Minutes {{sec}} Seconds
                  </div>
                  <div ng-hide='ques'>
     <h3>You got {{correct}} correct of {{questions.length}} questions</h3>
     <h3> Your Grade :  {{showscore}}% </h3>
     <h4>Exam Finished in Time :{{minuteleft}} Minutes {{sec}} Seconds</h4>
     <button ng-click='EndExam()'>End the Exam</button>
                  </div>
                  <br>
                  <br>
                </td>
              </tr>
            </table>
          </div>
        </section>
      </div>
    </div>
    <div id="Footer"></div>
  </div>
</body>
</html>

最佳答案

在 CSS 中你放置了一个 background-image,而在 JS 中你放置了一个 src!

代替

document.getElementById("online_start").src = "animatedthankyou.gif";

document.getElementById("online_start").style.backgroundImag‌​e = "animatedthankyou.gif";

关于javascript - 使用angularjs用另一个图像替换一个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39092565/

相关文章:

javascript - 从安全性 Angular 来说,在js代码中使用eval()与在chrome控制台中输入js代码有什么不同?

javascript - 如何使用 jQuery 添加另一个对象的高度作为边距?

css - ie7中的有序列表不是两位数的间距

javascript - Angularjs $scope - 如何通过 Controller 获得正确的范围(初学者)

javascript - 在 Angular2 中使用可观察的与其他组件的对话,不接收即将到来的值

javascript - Angular : ng-click parameter is undefined, 位于 ng-repeat 内部

javascript - 无法读取未定义的属性 'length'

javascript - 用JS用类替换多个元素的部分字符串

javascript - 找到key然后替换value

javascript - 使用 javascript 中的第 n 个子项指定图像在放置区域的位置