【发布时间】:2016-09-09 21:35:35
【问题描述】:
我现在已经设法使用以下代码获取输入字段值(属性值):
jQuery('input[ng-model="palette.name"]').each(function(index,item){
//console.log(angular.element(this.id).value);
console.log($(this).prop('value'));
});
但是,以下代码无法按计划工作。我想要实现的是在http://mcg.mbitson.com/#/ 上实现以下脚本以生成 LESS 格式的调色板:
(function() {
var colors = {}, main = {};
jQuery(".palette-colors").each(function() {
// This is the probem: color becomes undefined althougt it works fine in the code above
var color = jQuery(this > 'input[ng-model="palette.name"]').prop('value');
//color = jQuery(color).prop('value');
console.log(color);
color.trim().toLowerCase().replace(" ", "-");
colors[color] = {};
jQuery(this).find("md-list-item > div").each(function(index,item) {
if (index !== 5) {
var shade = jQuery(this).find('span[ng-bind="color.name"]').text().trim(),
hex = jQuery(this).find('span[ng-bind="color.hex"]').text().trim();
colors[color][shade] = hex;
}
});
main[color] = color + "-" + 500; //jQuery(this).find(".main-color .shade").text().trim();
});
var LESS = "";
jQuery.each(colors, function(name, shades) {
LESS += "\n\n";
jQuery.each(shades, function(shade, hex) {
LESS += "@" + name + "-" + shade + ": " + hex + ";\n";
});
if (main[name]) {
LESS += "@" + name + ": " + main[name] + ";\n";
}
});
console.log(LESS);
})();
google调色板“刮刀”原代码如下:
(function() {
var colors = {}, main = {};
jQuery(".color-group").each(function() {
var color = jQuery(this).find(".name").text().trim().toLowerCase().replace(" ", "-");
colors[color] = {};
jQuery(this).find(".color").not(".main-color").each(function() {
var shade = jQuery(this).find(".shade").text().trim(),
hex = jQuery(this).find(".hex").text().trim();
colors[color][shade] = hex;
});
main[color] = color + "-" + jQuery(this).find(".main-color .shade").text().trim();
});
var LESS = "";
jQuery.each(colors, function(name, shades) {
LESS += "\n\n";
jQuery.each(shades, function(shade, hex) {
LESS += "@" + name + "-" + shade + ": " + hex + ";\n";
});
if (main[name]) {
LESS += "@" + name + ": " + main[name] + ";\n";
}
});
console.log(LESS);
})();
【问题讨论】:
-
仅供参考 jquery 有一个函数
val()可以用来获取像$(this).val()这样的值 -
我试过这样:jQuery('input[ng-model="palette.name"]').val();哪个有效。一旦我在循环中尝试它: jQuery(this).find('input[ng-model="palette.name"]').val();它变得未定义。
标签: jquery web-scraping sass