在javascript函数中调用jquery标记

时间:2018-03-02 17:27:04

标签: javascript jquery

<script>    
    function addMoreAns() {
        $(".add").append("<h1>Hi</h1>")
    }
</script>

这是我在javascript函数中的jquery标记。 jquery标签在javascript函数下不起作用

2 个答案:

答案 0 :(得分:0)

您的代码必须有三件事情可以使用:

  1. 在使用任何JQuery代码之前,必须确保引用了JQuery库。
  2. 在HTML中,您必须拥有一个允许包含add类内容的元素。
  3. 您必须以某种方式调用您的功能。只是编写一个函数与运行它不是一回事。您必须决定何时运行该函数并设置您的代码以在此时调用它。
  4. 在下面的示例中,每次单击按钮时都会调用该函数。

    document.querySelector("input[type=button]").addEventListener("click", addMoreAns);
    
    function addMoreAns() {
      $(".add").append("<h1>Hi</h1>")
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="button" value="Click to Add">
    <div class="add"></div>

答案 1 :(得分:0)

您需要在页面的头部加载jQuery库

import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import os
import matplotlib.cm as cm
import matplotlib.colors as colors

def fibonacci_sphere(num = 1, radius = 1):
    """
    Returns the coordinates of 'num' points evenly distributed on a sphere.
    """
    rnd = 1.

    x_vals = []
    y_vals = []
    z_vals = []

    offset = 2./num
    increment = np.pi * (3. - np.sqrt(5.));

    for i in range(num):
        y = ((i * offset) - 1) + (offset / 2);
        r = np.sqrt(1 - pow(y,2))

        phi = ((i + rnd) % num) * increment

        x = np.cos(phi) * r
        z = np.sin(phi) * r

        x_vals.append(x)
        y_vals.append(y)
        z_vals.append(z)

    x_vals = [(radius * i) for i in x_vals]
    y_vals = [(radius * i) for i in y_vals]
    z_vals = [(radius * i) for i in z_vals]

    return np.array(x_vals), np.array(y_vals), np.array(z_vals)

##################
## CREATE DATA  ##  
##################
# Specify number of points on the sphere
num_points = 10000

# Create coordinates sitting on the surface of the sphere
x_sph, y_sph, z_sph = fibonacci_sphere(num = num_points)

# Create random data for the 'intensity' at each point on the sphere
intensity_sph = np.random.rand(num_points)


#########################################
## MAPPING INTENSITY VALUES TO COLOURS ##
#########################################
minima = min(intensity_sph)
maxima = max(intensity_sph)

norm = colors.Normalize(vmin = minima, vmax = maxima, clip=True) # what's clip?
mapper = cm.ScalarMappable(norm=norm, cmap=cm.jet)


############################
## CREATE FIGURE AND PLOT ##
############################
# Create figure and axes
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d', xlabel = 'x (m)', ylabel = 'y (m)', zlabel = 'z (m)')

# Plot scatter plot 
ax.scatter(x_sph, y_sph, z_sph, c = intensity_sph, cmap = cm.jet)

plt.show()