如何将代码显示为<code>

时间:2016-05-11 11:29:01

标签: javascript

I've created a button with some input fields for the user to enter the background color and text color for the buttons. This works perfectly. However I would then like to display this dynamic code in a code snippet, for people to copy and paste. (the code you see when you view source)

Here is a link to my page: https://appointuit.com/home/testbutton/

The js is:

function check(){
   document.getElementById('buttonOnline').style.background = document.myForm.colorType.value; 

document.getElementById('buttonOnline').style.color = document.myForm.colorTextType.value; 



}  
function code(){

     document.getElementById('snippet').textContent = document.myForm.buttonOnline.innerHTML;

}

1 个答案:

答案 0 :(得分:0)

你可以试试这个。我已经使用jQuery语法。

function *fridays13th() {
  var nextFriday = getNextFriday(new Date())

  while (true) {
    if (isFriday13th(nextFriday)) {
      yield new Date(nextFriday)
    }

    nextFriday = getNextFriday(nextFriday)
  }

  function isFriday13th(day) {
    return ((day.getDay() === 5) && (day.getDate() === 13))
  }

  function getNextFriday(d) {
    return new Date(d.setDate(d.getDate() + (5 - d.getDay() > 0 ? 5 - d.getDay() : 12 - d.getDay())))
  }
}


function generator(fn) {
  var res = []
  var g = fn()

  return {
    take: function(max) {
      for (var i = 0; i < max; i += 1) {
        var r = g.next()
        res.push(r.value)
      }
      return res
    }
  }
}


var fridays = generator(fridays13th).take(5)


document.body.innerHTML = '<pre>' + JSON.stringify(fridays, null, 4) + '</pre>';

我认为这里使用的ID对你来说是不言自明的。