Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的jquery代码未应用于最后一个div_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 我的jquery代码未应用于最后一个div

Javascript 我的jquery代码未应用于最后一个div,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在动态创建div,其中我用以下代码替换一些文本: $('p.project-category').each(function() { if($(this).text()==='Conferences --Events') { $('p.project-category').text('Conferences --Events').replaceWith('<p class="project-category">Conferences / Event

我正在动态创建div,其中我用以下代码替换一些文本:

$('p.project-category').each(function()
{
    if($(this).text()==='Conferences --Events')
    {
        $('p.project-category').text('Conferences --Events').replaceWith('<p class="project-category">Conferences / Events</p>')
    }  
    else if ($(this).text()==='Html Emails')
    {
        $('p.project-category').text('Html Emails').replaceWith('<p class="project-category">HTML E-Mails</p>')
    }
    else if ($(this).text()==='Periodicals Reports')
    {
        $('p.project-category').text('Conferences Events').replaceWith('<p class="project-category">Periodicals / Reports</p>')
    }   
    else if ($(this).text()==='Sales Collateral---standard')
    {
        $('p.project-category').text('Sales Collateral---standard').replaceWith('<p class="project-category">Sales Collateral (Standard)</p>')
    }
    else if ($(this).text()==='Sites Apps')
    {
        $('p.project-category').text('Sites Apps').replaceWith('<p class="project-category">Web Sites / Digital Apps</p>')
    }
});
这对所有人都有效,但最后一个部分除外。有什么原因不能起作用吗?

如果。。然后还有其他条件。你可以用开关把它弄清楚。。而是一个案例

另外,看起来您试图使用.text方法作为选择器,这是错误的。它用于设置/获取所选元素的文本

最后,您可以替换整个元素,而只替换其文本

$('p.project-category').each(function (el, ix) {
    var text = $(this).text().trim().toLowerCase();
    var newText = '';

    switch (text) {
        case 'conferences --events':
            newText = 'Conferences / Events';
            break;

        case 'html emails':
            newText = 'HTML E-Mails';
            break;

        case 'periodicals reports':
            newText = 'Periodicals / Report';
            break;


        case 'sales collateral---standard':
            newText = 'Sales Collateral (Standard)';
            break;

        case 'sites apps':
            newText = 'Web Sites / Digital Apps';
            break;

    }

    $(this).text(newText);
});

注意,我假设HTML标记中没有拼写错误,而且我不建议您在进行比较时依赖文本内容编写代码。

您设置了一个元素的文本,然后用另一个元素替换它?您听说过switch吗?原因是没有满足上一个条件。看起来您正在尝试使用.text作为选择器。如果是这样,那你就错了。看看@nd_macias comment,如果你在控制台的If块中运行代码,它能工作吗?如果是这样的话,他们是100%的钱。第一:我在项目早期就尝试过这个。我当前的代码是这个项目唯一的工作,因此是意大利面代码。第二:这实际上也有同样的问题,所以我假设XML中有一些东西。至少我知道现在这不是我的代码,所以还是谢谢你。