Есть такая функция:
create('arg')
В нее передаются аргументы,а можно как-нибудь сделать подобное:
create('arg').css({
//Тут должны быть css стили
})
.destroy(); //Удаление
И как принимать подобное?
Как сделать подобное javascript?
create('arg').css({ //Тут должны быть css стили }) .destroy(); //Удаление
Вот такой вариант:
//----------------------------------------------------------------
// Сама функция с эффектом chaining methods
// Вызов: create('tag .class #id') или create('tag .class')
// или create('tag #id') без класса
function create(args) {
// Два пробела если не хотите class, а только id
//
[this.tag, this.className, this.id] = args.split(' ')
// Создание элемента
this.elem = document.createElement(this.tag)
if (this.className) { //
this.elem.className = this.className.replace(/\./, '')
}
if (this.id) { //
this.elem.id = this.id.replace(/\#/, '')
}
// appendTo
this.appendTo = function(s) {
document.querySelector(s).appendChild(this.elem);
return this;
}
// css function
this.css = function({ ...args
} = {}) {
var s = ''
Object.keys(args).forEach(function(v) {
s += `${v}:${args[v]};`
})
this.elem.style = s
return this;
}
// find element
this.find = function(needle) {
this.found = this.elem.parentNode.querySelectorAll(needle)
return this
}
// destory if has found then found otherwise
this.destroy = function() {
if (this.found && this.found.length > 0) {
this.found.forEach(function(v, i) {
this.elem.parentNode.removeChild(v)
})
//this.elem.parentNode.removeChild(this.found)
} else
this.elem.parentNode.removeChild(this.elem)
return this
}
// text function....
this.text = function(t) {
this.elem.innerText = t
return this
}
return this;
}
//----------------------------------------------------------------
// Использование
create('div .added')
.css({
'border': '3px solid black',
'color': 'yellow',
'margin': '1px',
'padding': '.6em'
})
.text('<script>console.log("Something to say?")<\/script>')
.appendTo('.body')
.find('span.added') //
.destroy() // Удалит найденые объекты или сам элемент если не было поиска
//----------------------------------------------------------------
// Использование
create('div .done')
.css({'margin-top':'5px', 'border':'3px dotted green', 'float':'right'})
.appendTo('.body')
.added {
width: 200px;
height: 200px;
background: red;
}
.done {
width: 200px;
height: 200px;
background: yellow;
}
<div class="body">
<!-------- Some -------->
<span>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam,
feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper.
Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
</span>
<span class="added">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam,
feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper.
Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
</span>
</div>
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости