javascript How to dynamically generate the code in the JS file (script tag) based on the dynamically generated string
三叔
三叔 2017-06-12 09:29:03
0
2
727

My code is as follows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<p class="name name0">
    <p>test</p>
</p>
<p class="name name1">
    <p>test2</p>
</p>

<script>
    function func1(className) {
        return `$('${className}').find('p').text()`
    }
    
    for(var i=0;i<$('.name').length;i++){
        console.log(`name${i} p:`,func1(`.name${i}`));
    }
    //第一次循环生成语句: $('.name0').find('p').text();
    //第二次循环生成语句: $('.name1').find('p').text();
</script>
</body>
</html>

I don’t want to get the value of each P, just as an analogy. I want to know how to dynamically render the code inside <script> through the return value of func1()? Or how to dynamically render js files

I want this file of the current code to dynamically generate js code based on .name p, such as
When there are two .name p

<p class="name name0">
    <p>test</p>
</p>
<p class="name name1">
    <p>test2</p>
</p>

Then the final code in <script> should be

<script>
    $('.name0').find('p').text();
    $('.name1').find('p').text();

</script>

When there are three .name p

<p class="name name0">
    <p>test</p>
</p>
<p class="name name1">
    <p>test2</p>
</p>
<p class="name name2">
    <p>test3</p>
</p>

Then the final code in <script> should be

<script>
    $('.name0').find('p').text();
    $('.name1').find('p').text();
    $('.name3').find('p').text();
</script>

How can we achieve this effect?

三叔
三叔

reply all(1)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!