为什么要变成& 页面加载后?

Ste*_*ven 6 html special-characters knockout.js

我使用Firefox和页面上的工作时,我注意到,&变成&.

通常我可以通过使用来解决这个问题html_entitiy_decode()- 但在这种情况下,它无法正常工作.

然后我发现了这个.加载页面后会触发警报.

之前

在此输入图像描述

在此输入图像描述

使用PHP/Yii加载数据 - 而不是通过JS/Ajax加载.然而,我使用JS Knockout添加/删除品牌.

<ul data-bind="template: { name: 'brand-item-template', data: $root.brands}">
  <li>
    <span id="<?= $brand->id?>" data-bind="text: brand_name, attr: {'id': brand_id}"><?= $brand->name; ?></span>
    <a href="#" class="remove el-icon-remove-circle" data-bind="click: $parent.removeBrand"></a>
  </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

更新

我发现这个JS Knockout代码是改变的原因.但是在添加品牌之前不应该触发此代码.那为什么这会影响我&的?

这就是self.addBrand = function()改变.如果我删除此功能并保持原样,一切都很好.它可以是一个Knockout错误吗?

$('#store.manage-special-offers').exists(function(){
    Store.mangeSpecialOffers();
});

function manageBrandListModel() {
    var self = this;
    var store_id = $('.data-item-id').val();
    var exiting_list = $('.brand-list ul').clone();

    // Data
    self.brands = ko.observableArray(create_list(exiting_list));
    self.brand_name = ko.observable();
    self.brand_id = ko.observable();
    self.store_id = ko.observable(store_id);

    // This is the function that makes the chage
    self.addBrand = function() {

        if (self.brand_name() != "") {

            // Update DB
            $('#store.manage-brands').exists(function(){
                $.ajax({
                    url: site_url + '/associatebrand',
                    type: "POST",
                    dataType: 'json',
                    data: {
                        Brand: {
                           brandId : self.brand_id(),
                           storeId : self.store_id(),
                           add : true
                       }
                    },
                    success: function (data) {
                        // Add brand to GUI list
                        self.brands.push(new brand(self.brand_id(), self.brand_name()));
                        self.brand_name("");
                    }
                });
            });


        }
    }.bind(self);

    (...)


function create_list(exiting_list){
    var arr_list = [];

    $(exiting_list).find('li').each(function(e,li){
        var id = $(li).find('span').prop('id');
        var name = $(li).find('span').html(); // <--- This is the problem. Must be .text()
        arr_list.push(new brand(id,name));
     });
    return arr_list;
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释为什么会这样吗?

Ste*_*ven 0

这真的应该归功于 JeremyCook 和 Quentin 为我指明了正确的方向。

 $(exiting_list).find('li').each(function(e,li){
    var id = $(li).find('span').prop('id');
    var name = $(li).find('span').html(); // <--- This is the problem. Must be .text()
    arr_list.push(new brand(id,name));
 });
Run Code Online (Sandbox Code Playgroud)

我做错的是我使用了.html()文本并以 HTML 格式返回。改变这个来.text()解决我的问题。