为什么而不是帮助你每次投反对票。至少说一下有什么问题。
我尝试了一些在这里找到的建议,JSON.stringify()
但是当我检索数据属性时,我只得到第一个词“2003/09/01”。
谢谢你。
我有一个将字符串放入数组的循环。
var row = date + " - " + res + " - " + v.CSERV + " / " + v.CHAMB + " / " + v.LIT + " (" + v.TYPMVT + ")";
dataDetail.push(row);
数组的输出
[“2003/09/01 - 07:45 - 02/07/01(入学)”,“2003/09/03 - 15:00 - 01/46/01(转学)”,“2003/09/05 - 10:00 - 01/45/01 (转机) "," 2003/09/08 - 09:14 - 01/45/01 (转机) "," 2003/09/09 - 11:00 - 01/45 / 01(退出)“]
数据属性如下:
$('#Historique .items').append(`
<div class='item'>
<div class='content detail' data-detail=` + JSON.stringify(dataDetail) + `>
<p>` + "du " + dateD + " (" + value.UNI + ") au " + dateS + " (" + value.UNI + ") Nr° Séjour : " + value.NUM + " (" + value.TYP + ")" + `</p>
</div>
</div>`);
在 html 属性中使用 JSON 是不安全的。它肯定包含 qoutes。它也可能包含<>
会完全破坏您的标记的内容。
相反,您可以将其jquery.data
用作外部数据存储。
const item = $(... mark element with data-detail...) // create jquery object
item.find('[data-detail]').data('detail', data) // find marked element and put data to its expando object (avoiding html to dom phase)
下面的工作演示
$(function() {
const data = ["2003/09/01 - 07:45 - 02 / 07 / 01 (Admission)", "2003/09/03 - 15:00 - 01 / 46 / 01 (Transfert)", "2003/09/05 - 10:00 - 01 / 45 / 01 (Transfert)", "2003/09/08 - 09:14 - 01 / 45 / 01 (Transfert)", "2003/09/09 - 11:00 - 01 / 45 / 01 (Sortie)"]
const target = $('#target');
target.on('click', '.detail', function() {
console.log($(this).data('detail'))
})
const item = $(`<div class='item'>
<div class='content detail' data-detail>
<p>Whatever you do here. Click here to get data-detail</p>
</div>
</div>`)
item.find('[data-detail]').data('detail', data)
target.append(item)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="target" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句