Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   Сборка CSS кода (https://javascript.ru/forum/dom-window/53190-sborka-css-koda.html)

Q_BASIC 24.01.2015 14:20

Сборка CSS кода
 
Надо пройти по всем тегам input с классом css_editor:
HTML:
<input type="text" class="css_editor" data-ident=".wrap" data-edit="background" value="#fff" />

И составить одну строку:
Код:
Код:

.wrap { background: #fff; }
.element { color: #171717; }

И так далее
.wrap из date-ident
background из data-edit
#fff из value

UPD:

Должно быть так:
Код:
function array_event(item, i, arr){
window.styles = window.styles+"\n"+item.attr('data-ident')+' { '+item.attr('data-edit')+'; }'; // Добавляем данные в глобальную переменную
}
function plain_view(){
$('.edit_style').each(array_event); // Проходим по всем элементам с классом edit_style
}

Вызываем plain_view() и в глобальной переменной styles у нас стили из полей. Но почему то не работает


http://jsfiddle.net/ggxkhmt6/5/

danik.js 24.01.2015 15:52

Цитата:

Сообщение от Q_BASIC
Но почему то не работает

В консоль ошибок пишет:
Цитата:

ReferenceError: $ is not defined
Ты забыл объявить функцию $.

Q_BASIC 24.01.2015 16:00

$ это jQuery, его то я подключаю. Ошибка

Uncaught TypeError: undefined is not a function (index):29
plain_view (index):29
(anonymous function) (index):31
jQuery.event.dispatch jquery-1.9.1.js:3074
elemData.handle

http://jsfiddle.net/ggxkhmt6/5/

ruslan_mart 24.01.2015 16:15

Q_BASIC, jQuery не возвращает массив, поэтому метода forEach быть не должно. Используй просто each.

Q_BASIC 24.01.2015 16:19

Теперь ошибка на этой строчке:
window.styles = window.styles+"\n"+item.attr('data-ident')+' { '+item.attr('data-edit')+'; }';


Uncaught TypeError: undefined is not a function (index):210
array_event (index):210
m.extend.each jquery.js:2
m.fn.m.each jquery.js:2
plain_view (index):214
onclick

ruslan_mart 24.01.2015 16:24

function array_event(){
    window.styles += "\n"+ this.dataset.ident + ' { ' + this.dataset.edit + '; }';
}

function plain_view(){
    $('.edit_style').each(array_event);
}

Q_BASIC 24.01.2015 16:28

Всё, спасибо.

Сделал:
function plain_view(){
	window.styles = '';
	$('.edit_style').each(function(){
		window.styles = window.styles+"\n"+ $(this).attr('data-ident')+' { '+$(this).attr('data-edit')+': '+$(this).val()+'; }';
	});
	
	alert(window.styles);
}

ruslan_mart 24.01.2015 16:33

А почему не?

function plain_view(){
	window.styles = '';
	$('.edit_style').each(function(){
		window.styles += "\n"+ this.dataset.ident + ' { '+ this.dataset.edit + ': ' + this.value + ';}';
	});
	
	alert(window.styles);
}

danik.js 24.01.2015 17:12

dataset - не работает в старье.
Цитата:

Сообщение от Q_BASIC
Сделал: $(this).attr('data-ident')

Это делается так: $(this).data('ident')

ruslan_mart 24.01.2015 17:21

Цитата:

Сообщение от danik.js
dataset - не работает в старье.

Да и чёрт с ним. :)


Часовой пояс GMT +3, время: 04:52.