因此,您需要解析 HTML 以从中获取信息
例子
HTML:
<div id="my_content">
<div id="div1" class="reduce" style="visibility: visible;"></div>
<div id="div2" class="add" style="visibility: visible;"></div>
<div id="div3" class="add" style="visibility: visible;"></div>
<div id="div4" class="reduce" style="visibility: visible;"></div>
<div id="div5" class="reduce" style="visibility: hidden;"></div>
<div id="div6" class="add" style="visibility: hidden;"></div>
<div id="div7" class="add" style="visibility: hidden;"></div>
<div id="div8" class="reduce" style="visibility: hidden;"></div>
</div>
然后您可以将其解析为 JSON(使用 jQuery):
Javascript:
var parsed_data = {}
var order = 1
$('#my_content > div').each(function() {
// Some if else
var element_class = $(this).hasClass('reduce') ? 'reduce' :
( $(this).hasClass('add') ? 'add' : 'unkown')
parsed_data['data'+ order] = {
element_id: $(this).attr('id'),
display_class: element_class,
visibility: $(this).css('visibility'),
}
order++
})
然后你用 AJAX 调用你的控制器:
$.ajax({
url: "<CONTROLLER_URL>",
type: "POST",
dataType: 'json', // data returned, not data sent
data: parsed_data
}).done(function(data) {
console.log('Done')
})
红宝石控制器:
def your_controller_function
params.each do |key, div|
print "DIV N°#{key}"
print " Id #{element_id}"
print " Displayed class #{display_class}"
print " Visibility #{visibility}"
end
end
我希望它不会太乱,它有帮助。