आप कस्टम तत्वों का उपयोग करने के क्या आप वाकई उस में उपयोग नहीं किया जाएगा होने के लिए अपने टैग के नाम पर एक हाइफन -
डालने की आवश्यकता चाहते हैं भविष्य के मानक, उदाहरण के लिए: <ultimate-audio>
।
आपको कस्टम तत्व विनिर्देशों के का उपयोग करना चाहिए, जो का उपयोग document.registerElement()
के बजाय एक नया तत्व पंजीकृत करने के लिए करता है।
अंतिम, आप extends:'audio'
विकल्प का उपयोग नहीं कर सकते हैं यदि आप एक नया टैग बनाना चाहते हैं।
आप सभी आधुनिक ब्राउज़रों में class
परिभाषा का उपयोग कर सकते हैं: ulimate-ऑडियो के
सामग्री।एचटीएमएल:
<template>
<h3>UAI</h3>
<nav>
<button id=PlayBtn>Play</button>
<button id=StopBtn>Stop</button>
<input id=AutoplayCB type=checkbox disabled>Auto-Play
<div>Source : <output id=SourceOut></output></div>
</nav>
</template>
<script>
(function (owner)
{
class UAI extends HTMLElement
{
constructor()
{
super()
this.model = {}
this.view = {}
}
connectedCallback()
{
//View
this.innerHTML = owner.querySelector('template').innerHTML
this.view.autoplay = this.querySelector('#AutoplayCB')
this.view.source = this.querySelector('#SourceOut')
//Model
//autoplay
var attr = this.getAttribute('autoplay')
this.model.autoplay = typeof attr == 'string' && (attr == '' || attr == 'true')
//source
this.model.source = this.getAttribute('src')
//Model -> View
this.view.source.textContent = this.model.source
this.view.autoplay.checked = this.model.autoplay
//[play] event
var self = this
this.querySelector('#PlayBtn').addEventListener('click', function()
{
self.play()
})
this.querySelector('#StopBtn').addEventListener('click', function()
{
self.stop()
})
//init
if (this.model.autoplay)
this.play()
}
play()
{
this.model.audio = new Audio(this.model.source)
this.model.audio.play()
}
stop()
{
this.model.audio.pause()
}
set src (file)
{
console.log('%s.src=%s', this, file)
this.model.source = file
this.model.audio.src = file
this.view.source.textContent = file
if (this.model.autoplay)
this.model.audio.play()
}
set autoplay (value)
{
console.warn('autoplay=', value)
this.model.autoplay = (value === "true" || value === true)
this.view.autoplay.checked = this.model.autoplay
}
}
customElements.define('ultimate-audio', UAI)
})(document.currentScript.ownerDocument)
</script>
आप नियंत्रित की यूआई <template>
, आप एक <style>
तत्व जोड़ सकते हैं जहां में परिभाषित किया गया है। तो फिर तुम इस तरह अपने टैग का उपयोग कर सकते हैं:,
<link rel="import" href="ultimate-audio.html">
body
में नए टैग का उपयोग करें::
header
में कस्टम तत्व शामिल हैं,
<ultimate-audio id=UA src="path/file.mp3" autoplay></ultimate-audio>
तरीकों play()
, stop()
, और गुण src
और autoplay
को जावास्क्रिप्ट का उपयोग किया जा सकता है:
UA.src = "path/file2.mp3"
UA.play()
वेब घटकों, विशेष रूप से 'document.registerElement' में देखें ... आप ऑब्जेक्ट इंस्टेंस में डोम अट्रिबियों को बांधने के लिए सेटर्स का उपयोग कर सकते हैं और अटारी की निगरानी के लिए पुराने स्कूल उत्परिवर्तन ईवेंट (या एक उत्परिवर्तन ऑब्सर्वर) का उपयोग कर सकते हैं। – dandavis
https://developer.mozilla.org/en-US/docs/Web/API/Document/registerElement और http://webcomponents.org/articles/introduction-to-custom-elements/ –
आप पुन: प्रोग्राम करने का प्रयास कर रहे हैं ब्राउज़र (टैग बदलना) और यह नहीं किया जा सकता है। जहां आप तत्व जोड़ने की कोशिश कर रहे हैं, यह एक नई सुविधा है जो हर जगह उपलब्ध नहीं है या अस्थिर है। इन चीजों से अवगत रहें। – Rob